JQuery does not send POST data

So, I have a really simple form on a website that is completely designed to load its pages on AJAX. The only way to work with this form is to have it do the AJAX magic, so I started doing it. I had a testing form, so I knew that it all worked.

Here's the javascript for my form. The variable "fullpath" just tells me which page is currently loading, all pages are stored in the local folder "pages". It serializes the form and sends it to the server with some debugging alerts.

$(document).ready(function() { $("#regForm").submit(function(event) { alert($(this).serialize()); $.post("pages/" + fullpath, $(this).serialize(), function(data){ alert(data); }); return false; }); }); 

Here the form itself

 <form name="input" id="regForm"> <div class="form-field"><label>Username</label> <input type="text" name="username"/></div> <div class="form-field"><label>Password</label> <input type="password" name="password"/></div> <div class="form-field"><label>Confirm Password</label> <input type="password" name="password2"/></div> <div class="form-field"><label>Screen Name</label> <input type="text" name="screenname"/></div> <div class="form-field"><label>Email Address</label> <input type="text" name="address"/></div> <div class="form-field"><label>Group</label> <select name="usergroup"> <option value="0">Superuser</option> <option value="1">Admin</option> <option value="2">Moderator</option> <option value="3">Advmember</option> <option value="4">Member</option> <option value="5">Guest</option> </select> <br /> <label>Submit: </label><input type="submit" value="Submit" /> </div> </form> 

And here are some PHP that I put at the top of the page

 print_r($_POST); 

So, I fill out the form with some fictitious information, and I click submit. All data is displayed using

 alert($(this).serialize()); 

And then the call will succeed, and I will see the loaded form with

 alert(data); 

But, when I ask to print the $ _POST array in PHP, that’s all I get

 Array () 

So jQuery sends the data, they return the page, but for some reason the POST variables fail. Anyone want to lend a hand?

+4
source share
6 answers

This works in a script .

Are you sure fullpath is defined globally? I do not see any other possible source of errors in your code.

Change I see the real problem from your comments: 301 redirects not working through POST

If the status code 301 is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request if it cannot be confirmed by the user, as this can change the conditions under which the request was issued.

You need to remove this redirect problem, so "pages/" + fullpath points to a PHP script. It may also be a problem with your server configuration.

In the case of Apache, you can also look at this SO question .

+3
source

I put your snippets together in an html file and it worked for me, so the problem should be somewhere else in your code. (source: http://pastebin.com/y4Dfsepv )

+1
source

You did not specify the method = "post" in your form . If you do not specify, it will become the default = get method. So there are no values ​​in $ _POST, you can print_R ($ _ GET), and you will see the values ​​there.

Change the line below:

 <form name="input" id="regForm"> 

in

  <form name="input" id="regForm" method="post"> 

Update:

Response update as per comment. "Page /" + fullpath in $ .post may point to the wrong page, try to warn about it and check the server response in firebug. Make sure it points to the desired page using the full path to the php script, as shown below:

 $.post("http://localhost/pages/" + fullpath, $(this).serialize(), function(data) 
0
source

** Editing because we learned that server 301 redirects the code **

See this: https://mdk.fr/blog/post-data-lost-on-301-moved-permanently.html

301 Redirects lose POST content. So jQuery sends it together, but the server redirects it to the right place in the script without POST data. You need to find out where the correct location of the script is, and indicate this in a jquery call to avoid redirection.

0
source

you need to provide your direct link. "pages /" + fullpath. This is a problem, ajax cannot recognize your link when posting

0
source

You have no method = "POST" in your form.

-1
source

Source: https://habr.com/ru/post/1389854/


All Articles