Jquery $ .post empty array

I am using jQuery to submit a form to a php file, a simple script to check 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 am looking at the answer in firebug, this is an empty array. An array must have at least some value.

I can not understand why $_POST does not work in php file. Firebug shows a message containing content sent by email and account ID, only nothing was received in the php file.

The form:

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

Does anyone know what he is doing?

thanks


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

what's the reasonable way?

+4
source share
5 answers

$.post() passes the data to the base call $.ajax() , which sets application/x-www-form-urlencoded by default, so I don’t think it is.

You can try the following:

 var post = $('#myForm').serialize(); $.post("includes/verify.php", post, function(data) { alert(data); }); 

calling serialize() will capture all the current data in form.myForm .

+14
source

I was bitten by the same problem, and I found a solution that Owen did not like. You serialize the object yourself, and jQuery should do it for you. You can also do $ .get () in this case.

I found out that in my case it was a server redirect from / mydir to / mydir / (with a slash), which did not work with the POST array. Request sent to index.php inside / mydir

This was on the local machine, so I could not verify the HTTP traffic. I would have known earlier if I had done this.

+4
source
 application/x-www-form-urlencoded 

Here is your answer. Upon receiving the message, you are simply looking for variables in the $ _POST array. Do you really want $ _REQUEST. Unlike the name, $ _POST contains the input variables represented in the request body, regardless of the sending method. $ _GET contains variables parsed from the query string of the URL. If you only need variables, use the global $ _REQUEST.

If you expect to receive file downloads, then you want to create a new array, including the contents of $ _FILES:

$ arguments = $ _REQUEST + $ _FILES;

+1
source

I tried this function from Owen, but also received an empty warning. Strange, but I noticed that it displays a query string and returns an empty warning. Then I will send again and it will warn about the correct message values.

Also the field names were specified in html using the id attribute (as was done in the jquery tutorial that I read). This prevented the serialization of form fields. When I switched the identifier to the name, it solved my problem.

In the end, I ended up with $ .ajax, because I did what I was looking for.

0
source

I had a case where I used jQuery to disable all inputs (even hidden ones that I wanted) just before using jQuery to submit the form. I changed my jQuery to disable input like "button" and now hidden vars are published when the form is submitted! It seems that if you turn off hidden input, its values ​​will not be sent with the form!

Modified by:

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

in

 $('input[type=button]').attr('disabled',true); 
-1
source

All Articles