I'm trying to access some data I've sent to this page (pincode.php), by using the $_POST[] array. However, it seems the variable isn't set, even though the network tab shows the data got succesfully sent.
The code I use to get the value from the $_POST[] array (in the pincode.php file):
if (isset($_POST['buy'])) {
$id_buy = $_POST['buy'];
echo $id_buy;
}
The problem is the if-statement doesn't fire, so apperently $_POST['buy'] isn't set.
Does anyone know a possible cause?
UPDATE:
As a test, I've created the file: test.php, with the following content:
This creates the following result and I still don't get the echo:
I am getting the same issue here, am I missing something important?
Related
I have a problem that occurs when I try to send a form, if a particular field type is present, the page returns a time out.
I'm using KnockoutJS's mapping plugin to map an object coming from the server. I'm able to modify the object and I want to send it back.
To do so, I created a computed function that returns the result of ko.toJSON to which I pass this particular object. Then, this value is put in an hidden input to be sent back to the server:
<input type="hidden" name="a" data-bind="value: exportToJSON()" />
When I'm sending the form with this input field, I get an error 7:
Error 7 (net :: ERR_TIMED_OUT): The operation timed out.
Here's a JSFiddle representing my code
http://jsfiddle.net/etiennenoel/4EXSy/17/
I suspect that the problem is caused because the data in the field sent via POST is not escaped ?
Update 1
Someone suggested to use ko.mapping.toJSON. However, doing that results in an empty string, see it here: http://jsfiddle.net/etiennenoel/4EXSy/18/
Update 2
Now, results input is not empty, thanks to #abc123: http://jsfiddle.net/etiennenoel/4EXSy/19/.
However, I still get a timeout when sending the POST Data. You can see the POST data that is sent when I send my form in my code: http://pastebin.com/hNRm4zdZ
Update 3
I'm using symfony2 and I'm starting to think that the problem might be linked to something in symfony2 since when I copy and paste the form on a simple .html file, the data gets sent...
Update 4
I deleted Symfony2 dev.log, clicked on the button to send the form, got the time out error and unfortunately, the log is still empty.... Also, all the php and apache logs do not show anything...
Update 5
I finally decided to test my symfony website on another server and it works on this other server... Now this is getting weird, why isn't it working in my local server ? I'm using MAMP Pro as a local server
You are returning ko.mapping.toJSON(self.playersEvaluation) you cannot access a property of knockout without calling the function since it is actually a function.
To get it to return proper JSON please do the following:
function appViewModel() {
var self = this;
self.playersEvaluation = ko.observableArray();
self.exportToJSON = ko.computed(function() {
return ko.mapping.toJSON(self.playersEvaluation())
}, this);
}
JSFiddle: http://jsfiddle.net/abc123/WReza/1/
To use Console Easily: http://jsfiddle.net/abc123/WReza/1/embedded/result/
Luckily since you are using the ko.mapping plugin this will work because it does the following:
All properties of an object are converted into an observable. If an update would change the value, it will update the observable.
Arrays are converted into observable arrays. If an update would change the number of items, it will perform the appropriate add/remove actions. It will also try to keep the order the same as the original JavaScript array.
Taken from ko.mapping
I'm creating a global high score table. The JavaScript sends the username and score to the PHP through some AJAX functions and POST. The PHP script then takes the variable, and compares the score values to the file on the server. It then puts the new score where it should go on the score table, and rewrites the file.
I had it working last night, but now, when I call the function, it displays my code instead, and firebug gives the error, "No element found". I'm not sure what I did between then and now, but I just can't figure this one out.
This is my first time using PHP and AJAX, so I'm not sure where or what the error is.
Any help would be appreciated!
The exact error:
Timestamp: 4/13/2013 1:59:18 PM
Error: no element found
Source File: file:///C:/wamp/www/ajax.php
Line: 84, Column: 3
Source Code:
?>
If I cannot help you solve this issue, I will delete this answer, but I wanted to post it like this so you can better read my suggestions.
Start by changing your HandleResponse function to this:
function HandleResponse(response)
{
console.log(response);
document.getElementById('ResponseDiv').innerHTML = response;
}
and post the result that appears above the error, in firebug.
What we are doing here is adding a way for us to see if any data was actually returned from the AJAX request. A call to console.log tells the browser to print the given argument to the console (in your case, firebug).
UPDATE 1
In your comments, you stated that nothing displayed when you logged the value of response. This means that you received nothing back from the server (in terms of data, at least).
My next suggestion is that you change the call to MakeRequest to the following:
<input type='button' id="test" onclick='MakeRequest("*");' value='Global Highscore Table'/>
Like before, leave the console.log line in the HandleResponse method, and post the result that appears above the error in firebug.
Here, we are setting your MakeRequest to make a request that passes a wild-card query parameter. At the time of this suggestion, I did not realize that the requested file expected two arguments, nor that passing a wild-card would error the script. However, this was still a good thing to do, as we found a new error, which tells us that the request is being received.
UPDATE 2
Now, cut and paste everything from your "ajax.php" file to your notepad or another, similar application. Then, set the following to be the only content of the "ajax.php" file:
<?php echo "Request received and response sent"; ?>
Again, post the result of the console.log.
Here, we are taking a step back from fixing the complex code, and we are going back to the basics. All we want to do, here, is verify that we can both send the request and receive a response.
If "Request received and response sent" is successfully returned as the response of the request, then we know that the error lies in ajax.php, and not in the request. If it is not, we know that there is a problem with the request (be it a server issue or the request, itself). If the latter is the case, there could still be a problem with ajax.php, but we must first fix the request.
UPDATE 3 (final solution)
Issue was found to be due to not correctly running on the local server.
I'm working on a system and currently try to implement a script that another (external) system can post to some data so I can store them.
I have no control over the external system - I can just trigger it to post data to my system, giving it my script's url.
Looking at firebug when the post happens, I can see the data posted, something that looks like this:
or (urldecoded)
content={"sex":"male","person":{"name":["chris"],"mbox":["mailto:name.lastename#gmail.com"]}}
&Content-Type=application/json
&auth=DDE233H76BGN
My problem is that when trying to get these parameters in my script, $_POST (and $_REQUEST) is always empty!
I've tried var_dump($_POST) or echo file_get_contents("php://input");, but I don't see any contents.
What am I missing here?
I don't know if response/request headers are needed to get something out of it, I show them here just in case
Edit:
My script now consists of a single line of code, like:
<?php
var_dump($_POST);
?>
or
<?php
echo file_get_contents("php://input");
?>
both of them give me absolutelly nothing :s
The data should be accessed using $arr= json_decode($_POST['content']); ... but you have another problem here.
A detail is missing:
... how can firebug can show you the content of a $_POST that is sent from an external system to your website ( aka: the request does not go through your browser, but probably through an CURL request originating from the external server ). Obviously, I don't get something here.
What I see is a POST request sent from your browser ( in javascript ), made by your website.
Your question miss a crucial detail, I'm just not sure what it is.
Hint:
Try to put an echo 'test'; just before your var_dump, I have the feeling that you may not be debugging the page that is really called by the Ajax POST request that we see in Firebug. A little routing problem ?
Lets look at RFC 1945 to see what "parameter" is
parameter = attribute "=" value
attribute = token
token = 1*<any CHAR except CTLs or tspecials>
CTL = <any US-ASCII control character
(octets 0 - 31) and DEL (127)>
So i suppose "Content-Type=application/json" is not a valid part of POST because "-" is not of CTLs
You should try looking at the raw POST data variable:
echo $HTTP_RAW_POST_DATA;
i have found some similar topics on this issue, but nothing for my case:
i send some post data via form, but the $_POST array is always empty (not null). but when i add a "die;" or "exit;" after var_dump($_POST); i can see all data sent.
maybe its relevant to know, that this is inside a (shopware) plugin which is called on "onPreDispatch".
thanks for any help
Your (shopware) plugin probably uses output buffering. Which means it will gather all the ecoes and prints until you call ob_flush() which prints all the buffer.
die() function, apart from everything else, also flushes the buffer when called.
So, if you do ob_flush() after your echo you should get the needed result.
the problem was the redirect, which resetted the post although it was after i read the parameters in the request.
i did not know, that shopware saves the desired data (paymentID) in the database. so i switched my plugin back to "onPostDispatch" (this way it will be called after all other actions, one of them saves the data in db). now i can just read the db and get the same data, which was initially in the post array.
i tried to be "the first" who reads the post, but could not work it out. now i am the last who reads it and it works fine.
thanks for all answers! the clue here was "redirect".
This may be a n00b topic but, anyways, I have been having a rather difficult and strange time with this bug. Basically I was working on a controller method for a page that displays a form. Basically, I was debugging the form's submitted data by var dumping the form input using codeigniter's $this->input->post function like so:
var_dump($this->input->post('first_name'));
Every other day this has worked, but today I was finding that when I dumped post variables to the browser it would return false as if they had no values even though they did have values when I submitted the form. Then I tried accessing the variables through PHP's POST superglobal array directly like so:
var_dump($_POST['first_name']);
and that returned empty as well so then I tried dumping the entire post array like so:
var_dump($_POST);
and it was empty as well despite the fact that I filled out the entire form. Nevertheless, the records in my MySQL database were being updated (which means that the form was submitting even though my $_POST variables appeared empty).
Also, I reasoned that normally, if I var dumped variables in the controller function before a redirect function call that it should give me a 'Headers already sent' error but it never did. It just redirected me to the supposed success page instead of dumping my variables.
So for the about 2 hours I thought that my POST data wasn't being sent and re-checked the code for errors and began commenting out statements one by one until I could find the culprit statement in my script.
Finally, I commented out a chunk of code that sets a success message an redirects, like so:
/*
if($record_updated_successfully)
{
$this->session->set_flashdata('success', $this->lang->line('record-updated-successfully'));
}
redirect('admin/success_page');
*/
and only then did the script start dumping out all my previous variable dumps using codeigniter's $this->input->post function as well as the $_POST superglobal array.
So ok, if the script does indeed redirect me despite the variable dumps sending output before headers are sent then I can see why the $_POST variables would appear empty.
So then the real question is why the script would still redirect despite my sending of output before headers are sent? Has anyone ever experienced this?
Any help with this would be appreciated.
EDIT: with respect to loading the view here's a simplified version of my script looks like
with the debugging var dump statements:
function some_controller_method() {
var_dump($this->input->post());
var_dump($_POST);
// some code
if($this->input->post('form_action') == 'update record') {
// code that runs when the form is submitted
/*
* ...
*/
if($record_updated_successfully)
{
$this->session->set_flashdata('success', $this->lang->line('record-updated-successfully'));
}
redirect('admin/success_page');
}
$this->load->view('my-view-file.php');
}
While I can't be sure, I'm going to assume you were outputting things like the var_dump() in your view file. A view is not executed at the time you call it, for example:
$this->load->view('some_view');
echo "hi!";
In a controller will not result in the contents of some view followed by "hi". It will results in "hi" followed by the contents of some view. The view is actually output after everything else in the controller has run.
This is the only thing that comes to mind with the information you've presented. I'd have to see more code to offer a different diagnosis.
I had "the same" problem and I found an unset() function in a loop in my code. Perhaps this will help.