post data missing after php parses raw post to post variable
-
I ran across this a few years ago and found I could just handle the raw post data myself. Recently I had to write a script to handle an ajax call, from grease monkey in fact, and the raw post data is there and the post variable is set but it is blank. I would prefer to use the post variable instead of having to manually parse the data. A sample html/js/php fragments of what I am doing follows:
<?php
fwrite($fp,file_get_contents("php://input"));
?>I can read the raw post data. But when I use something like
<?php
echo $_POST['name'];
?>it is blank My source data is built via js like this...
js...
var postvar = "name="+encodeURI(name)+"&markup="+encodeURI(markup)
...jsName is a identifier and markup is a select html code. I am curious if anyone has figured out why php fails to parse the raw data sent to a page.
-
I ran across this a few years ago and found I could just handle the raw post data myself. Recently I had to write a script to handle an ajax call, from grease monkey in fact, and the raw post data is there and the post variable is set but it is blank. I would prefer to use the post variable instead of having to manually parse the data. A sample html/js/php fragments of what I am doing follows:
<?php
fwrite($fp,file_get_contents("php://input"));
?>I can read the raw post data. But when I use something like
<?php
echo $_POST['name'];
?>it is blank My source data is built via js like this...
js...
var postvar = "name="+encodeURI(name)+"&markup="+encodeURI(markup)
...jsName is a identifier and markup is a select html code. I am curious if anyone has figured out why php fails to parse the raw data sent to a page.
cjoki wrote:
<?php
echo $_POST['name'];
?>Try
print_r( $_REQUEST );
to see what was sent. Without seeing the rest of your javascript I can only guess at what the problem is. The most common problems are that the http request was opened with 'GET' rather than 'POST' eg.http_request.open('POST', url, true);
Or the parameters weren't sent eg.http_request.send(postvar);
Or I may have missed the point, so please clarify.If at first you don't succeed, you're not Chuck Norris.
-
cjoki wrote:
<?php
echo $_POST['name'];
?>Try
print_r( $_REQUEST );
to see what was sent. Without seeing the rest of your javascript I can only guess at what the problem is. The most common problems are that the http request was opened with 'GET' rather than 'POST' eg.http_request.open('POST', url, true);
Or the parameters weren't sent eg.http_request.send(postvar);
Or I may have missed the point, so please clarify.If at first you don't succeed, you're not Chuck Norris.
fly904, thanks for the response but I found my error. Using the GM_xmlhttpRequest object I mistakely set the content type to...
GM_xmlhttpRequest({
method: 'POST',
url: '[URL deleted for post]/copy_prdt.php',
data: postdata,
headers:
{
'Content-type': 'x-www-form-urlencoded'
},
onload: function(responseDetails)
{
alert('Request returned ' + responseDetails.status + ' ' + responseDetails.statusText + '\n\n' +' data:\n' + responseDetails.responseText);
},
onError: function(responseDetails)
{
alert('Request complained ' + responseDetails.status + ' ' + responseDetails.statusText + '\n\n' +' data:\n' + responseDetails.responseText);
}
});and it needed to be...
GM_xmlhttpRequest({
method: 'POST',
url: '[URL deleted for post]/copy_prdt.php',
data: postdata,
headers:
{
'Content-type': 'application/x-www-form-urlencoded'
},
onload: function(responseDetails)
{
alert('Request returned ' + responseDetails.status + ' ' + responseDetails.statusText + '\n\n' +' data:\n' + responseDetails.responseText);
},
onError: function(responseDetails)
{
alert('Request complained ' + responseDetails.status + ' ' + responseDetails.statusText + '\n\n' +' data:\n' + responseDetails.responseText);
}
});It is odd, I did not get any error messages and the script seem to work fine, except for the post variable not having the data parsed into it. I can only guess that the php parser was confused and quietly aborted the parsing of the post data.
-
fly904, thanks for the response but I found my error. Using the GM_xmlhttpRequest object I mistakely set the content type to...
GM_xmlhttpRequest({
method: 'POST',
url: '[URL deleted for post]/copy_prdt.php',
data: postdata,
headers:
{
'Content-type': 'x-www-form-urlencoded'
},
onload: function(responseDetails)
{
alert('Request returned ' + responseDetails.status + ' ' + responseDetails.statusText + '\n\n' +' data:\n' + responseDetails.responseText);
},
onError: function(responseDetails)
{
alert('Request complained ' + responseDetails.status + ' ' + responseDetails.statusText + '\n\n' +' data:\n' + responseDetails.responseText);
}
});and it needed to be...
GM_xmlhttpRequest({
method: 'POST',
url: '[URL deleted for post]/copy_prdt.php',
data: postdata,
headers:
{
'Content-type': 'application/x-www-form-urlencoded'
},
onload: function(responseDetails)
{
alert('Request returned ' + responseDetails.status + ' ' + responseDetails.statusText + '\n\n' +' data:\n' + responseDetails.responseText);
},
onError: function(responseDetails)
{
alert('Request complained ' + responseDetails.status + ' ' + responseDetails.statusText + '\n\n' +' data:\n' + responseDetails.responseText);
}
});It is odd, I did not get any error messages and the script seem to work fine, except for the post variable not having the data parsed into it. I can only guess that the php parser was confused and quietly aborted the parsing of the post data.
cjoki wrote:
I can only guess that the php parser was confused and quietly aborted the parsing of the post data.
Debugging Ajax is a bitch, as it doesn't always throw an error, because technically it works although not the way you want it :( .
If at first you don't succeed, you're not Chuck Norris.
-
cjoki wrote:
I can only guess that the php parser was confused and quietly aborted the parsing of the post data.
Debugging Ajax is a bitch, as it doesn't always throw an error, because technically it works although not the way you want it :( .
If at first you don't succeed, you're not Chuck Norris.
I tend to use an "output" box for this that just says what the code is doing step by step on js side and Firefox Error Console. But some errors do still seem to hide. A function you may know: To read headers sent using PHP you can use:
<?php
var_dump(headers_list());
?> -
I tend to use an "output" box for this that just says what the code is doing step by step on js side and Firefox Error Console. But some errors do still seem to hide. A function you may know: To read headers sent using PHP you can use:
<?php
var_dump(headers_list());
?>var_dump prints the results to a browser. In an ajax based system this can be messy. Although I could have used var_export, I instead used fwrite($fp,file_get_contents("php://input")); so to write the contents of the raw post data before the parser works on filling the $_POST array. Doing it this way allows the server side script of a ajax solution to continue and return some value so your javascript can shutdown gracefully while allowing you to capture the content. I also use a firefox addon called httpfox so I can observe the communication between my browser and the server.
-
var_dump prints the results to a browser. In an ajax based system this can be messy. Although I could have used var_export, I instead used fwrite($fp,file_get_contents("php://input")); so to write the contents of the raw post data before the parser works on filling the $_POST array. Doing it this way allows the server side script of a ajax solution to continue and return some value so your javascript can shutdown gracefully while allowing you to capture the content. I also use a firefox addon called httpfox so I can observe the communication between my browser and the server.
:thumbsup: httpfox :thumbsup: Been looking for something like that for a while. Plain & Simple.
-
:thumbsup: httpfox :thumbsup: Been looking for something like that for a while. Plain & Simple.