views:

11267

answers:

7

Hey,

I'm using jQuery to post a form to a php file, simple script to verify user details.

var emailval = $("#email").val();
var invoiceIdval = $("#invoiceId").val();

$.post("includes/verify.php", 
       {invoiceId:invoiceIdval , email:emailval },
       function(data) { 
          //stuff here.
});

PHP Code:

<?php
print_r($_POST);
?>

I look at the response in firebug, it is an empty array. The array should have at least some value.

I can not work out why the $_POST isn't working in the php file. Firebug shows the post to contain the contents posted, email and invoice id, just nothing is actually received in the php file.

The form:

<form method="post" action="<?=$_SERVER['PHP_SELF']; ?>" enctype="application/x-www-form-urlencoded">

Anyone know what its doing?

thanks

A: 

found this - http://www.bradino.com/php/empty-post-array/

that a sensible route to go?

Neil
+1  A: 
application/x-www-form-urlencoded

There's your answer. It's getting posted, you're just looking for the variables in $_POST array. What you really want is $_REQUEST. Contrary to the name, $_POST contains input variables submitted in the body of the request, regardless of submission method. $_GET contains variables parsed from the query string of the URL. If you just want submitted variables, use the $_REQUEST global.

If you expect to be receiving file uploads, than you want to create a new array including the contents of $_FILES as well:

$arguments = $_REQUEST + $_FILES;

Douglas Mayle
+8  A: 

$.post() passes data to the underlying $.ajax() call, which sets application/x-www-form-urlencoded by default, so i don't think it's that.

can you try this:

var post = $('#myForm').serialize();    

$.post("includes/verify.php", post, function(data) { 
    alert(data);
});

the serialize() call will grab all the current data in form.myForm.

Owen
doh, that works, thanks, i can get to the values..
Neil
no problem, glad you got it working :)
Owen
A: 

Hey Douglas,

i tried that, the $_request was returnign session info and some other bits, so i removed the enctype, switched it from jQuery $.post to $.get and well that seems to allow me to $_GET the contents of the array, and use them in the verfiy.php file. ah well, i'll use get instead. doesnt really matter to me either way.

thanks though.

Neil
A: 

Owen,

The alert is empty, unless i print out the $_POST in verify.php, but then the respons ein firebug shows an empty array...

cheers.

Neil
A: 

I got bitten by the same issue, and I find the solution Owen gives not appropriate. You're serializing the object yourself, while jQuery should do that for you. You might as well do a $.get() in that case.

I found out that in my case it was actually a server redirect from /mydir to /mydir/ (with slash) that invalidated the POST array. The request got sent to an index.php within /mydir

This was on a local machine, so I couldn't check the HTTP traffic. I would have found out earlier if I would have done that.

A: 

I tried the given function from Owen but got a blank alert as well. Strange but i noticed it would output a query string and return a blank alert. Then i'd submit again and it would alert with correct post values.

Also had the field names set in the html using the id attribute (which was how it was done in a jquery tutorial I was following). This didn't allow my form fields to serialize. When I switched the id's to name, it solved my problem.

I ended up going with $.ajax after all that since it did what I was looking for.

dardub