4

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


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

that a sensible route to go?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Neil
  • 55
  • 1
  • 3
  • 6

5 Answers5

14

$.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
  • 82,995
  • 21
  • 120
  • 115
4

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.

1
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
  • 21,063
  • 9
  • 42
  • 57
  • This didn't matter for me. POST and REQUEST were not working. I am not positive what the problem is, but serialize (above) seems to work and I just switched to $.get instead. – Jake May 29 '12 at 14:22
0

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
  • 3,166
  • 5
  • 29
  • 31
-1

I had a case where I was using jQuery to disable all the inputs (even the hidden ones I wanted) just before using jQuery to submit the form. I changed my jQuery to only disable the "button" type inputs and now the hidden vars are posted when the form is submitted! It seems that if you set a hidden input to disabled its values aren't posted with the form!

Changed:

$('input').attr('disabled',true);

to:

$('input[type=button]').attr('disabled',true);
Josh P
  • 1,325
  • 14
  • 12