$_POST is empty , what is going wrong

Hi
I am posting an array from page-1 as follows:

var params = {};
params[‘id’] = idtval;
params[‘pswd’] = pswd;
params[‘inithint’] = inithint;
params[‘fnhint’] = fnhint;
params[‘lnhint’] = lnhint;

var paramsJ = JSON.stringify(params);

xmlhttp.open(“POST”,“ajax-d.php”,true);

xmlhttp.setRequestHeader(“Content-type”, “application/x-www-form-urlencoded”);
xmlhttp.setRequestHeader(“Content-length”, paramsJ.length);
xmlhttp.setRequestHeader(“Connection”, “close”);

xmlhttp.send(paramsJ);

In page-2, using FireBUG, it appears in the header as 4 good parameters.

But I cannot seem to get anything out of $_POST. It appears empty.
I have tried a lot of very rookie syntax attempts as follows and nothing works:

<?php $json = stripslashes($_POST["id"]); $output = json_decode($json); echo $output; $id = $_POST["paramsJ"]["id"]; echo "starts here: ", $id; echo $id; $id = $_POST['id'][0]; echo "2nd echo", $id; echo $id; $id = htmlspecialchars( $_POST['id']); echo "3rd echo", $id; echo $id; $id = $_POST['id']; echo "4th echo", $id; echo $id; echo "Dump here: "; var_dump($_POST['paramsJ']); Please help.

Oops. I meant to write 5(five) good parameters.
Cheers

If you want to do it like this you should really post the fields in another way, read more here

[hr]

Or you can catch what you have now with reading the raw data

[php]<?php

$data = json_decode(file_get_contents(‘php://input’));
print_r($data);[/php]

[hr]

Best solution, use Jquery… It’s just way easier to both write and read.

[php]$("#yourForm").submit(function(event) {
event.preventDefault();

var $form = $(this),
url = $form.attr( “action” ),
formData = {
id: $form.find( “input[name=‘idtval’]” ).val(),
pswd: $form.find( “input[name=‘pswd’]” ).val(),
inithint: $form.find( “input[name=‘inithint’]” ).val(),
fnhint: $form.find( “input[name=‘fnhint’]” ).val(),
lnhint: $form.find( “input[name=‘lnhint’]” ).val()
}

var posting = $.post( url, formData );

posting.done(function(response) {
console.log(response)
});
});[/php]

Thank you JimL.
Your suggestion of:

$data = json_decode(file_get_contents(‘php://input’));

worked perfectly. I would never have found that in a year of looking but need to research it now.

Also, your final suggestion of JQuery looks interesting. I will have to research that to understand the form and event pointers.
Thanks again.

Sponsor our Newsletter | Privacy Policy | Terms of Service