4

So I have a really simple form on a website that's entirely AJAX based for loading its pages. The only way for this form to work would be for it to do some AJAX magic as well, so I set about doing it. I had the form tested so I knew it all worked.

Here's the javascript for my form. The variable "fullpath" just tells me what page is loaded at the moment, all of the pages are stored in the local "pages" directory. 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's 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's some PHP I put at the beginning of the page

print_r($_POST);

So I fill the form with some bogus info, and I press submit. All of the data is displayed with the

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

And then the call is successful and I see the loaded form with my

alert(data);

But, where I ask to print the $_POST array in PHP, this is all I get

Array ()

So jQuery is sending the data, it's getting the page back, but for some reason the POST variables aren't going through. Anyone care to lend a hand?

Pinpickle
  • 984
  • 2
  • 9
  • 18
  • `So jQuery is sending the data` --- how do you know that? Have you checked it with firebug? – zerkms Jan 08 '12 at 01:25
  • Didn't know that was possible. Just checked, according to Firebug all of the variables are being sent. – Pinpickle Jan 08 '12 at 01:38
  • where are you trying to see the POST data? What page? Have you tried sending it back through the request into the success function data? You can pass back arrays using `json_encode` in php to quickly send back the array. – Brombomb Jan 08 '12 at 01:47
  • In my php page I've just got print_r($_POST) to display the entire array and it's giving me an empty array. – Pinpickle Jan 08 '12 at 01:56

6 Answers6

3

This works in a Fiddle.

Are you sure that fullpath is defined globally ? I don't see any other possible source of errors in your code.

Edit: I can see the actual problem from your comments: 301 redirects don't work through POST:

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

You need remove this redirect thing, so "pages/" + fullpath directly points to the PHP script. This could also be a problem with your server configuration.

In case of Apache, you might also want to have a look at this SO question.

Community
  • 1
  • 1
copy
  • 3,301
  • 2
  • 29
  • 36
  • Thanks for teaching me how to doa post on jsfiddle. I tried converting this to a fiddle, but didn't know how :) – Brombomb Jan 08 '12 at 01:50
  • Your link showed no more functionality than what I've already got working. My fullpath variable works fine, and I can make the request to the page successfully. The problem is that my POST variables aren't going through. – Pinpickle Jan 08 '12 at 01:50
  • Unless I'm missing something - am I missing something in your jsfiddle example? – Pinpickle Jan 08 '12 at 01:55
  • @Pinpickle: If you actually get a second alert, this has to be an issue with your server. I can not reprocuce this even with `print_r($_POST);` on my local server. Maybe you check `"pages/" + fullpath` and see if it points to the right script. – copy Jan 08 '12 at 01:59
  • It's definitely pointing to the right script but Firebug is showing a 301 Permanently Moved when I get my response. This is really strange because forms are working everywhere else. – Pinpickle Jan 08 '12 at 02:13
  • I'm running this off a friend's server, I'll link him to this page and see if he can do anything about it. I'm guessing it's possible that the problem can also lie in my .htaccess files? I'll take a look through them... Thanks a lot for this! – Pinpickle Jan 08 '12 at 02:28
  • You were completely right. I was running a GET query on the page as well and there was a redirect from page?query to page/?query So I just changed the location by adding that slash and it worked! Thank you! – Pinpickle Jan 08 '12 at 02:35
1

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

  • So I think you're right in saying that something is up somewhere else. Do you know what could cause this to happen? – Pinpickle Jan 08 '12 at 02:10
0

you need to chance your direct link."pages/" + fullpath. That is a problem, ajax can't recognize your link when you post

fish
  • 1
  • 1
0

You have not specified method="post" in your form. If you do not specify, it becomes method="get" by default. So there is no values in the $_POST, you can print_R($_GET) and you will see values there.

Change the below line from:

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

to:

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

Update:

Updating the answer as per the comment. The "pages/" + fullpath in $.post might be pointing to the wrong page, try alerting it and check server response in firebug. Make sure it is pointing to the page you want else use the full path to the php script like below:

$.post("http://localhost/pages/" + fullpath, $(this).serialize(), function(data)
Ghost-Man
  • 2,179
  • 1
  • 21
  • 25
  • As explained in the other answer - I don't need method="post" as I'm using jQuery. I've already checked with Firebug and it IS making a POST request. I just checked with method="post", it doesn't make a difference. – Pinpickle Jan 08 '12 at 01:42
  • ok, you can check in firebug as well if you are getting any response or not and whether the status is ok(200) or some other error. If the path to the file pointing to somewhere else you might be getting bogous response from the server and you may need to use the full path like "http://localhost/pages/"+ fullpath, and in my answer i tried to point out why print_r($_POST) prints an empty array. – Ghost-Man Jan 08 '12 at 01:49
  • Ooh, thank you. I'm not getting 200 ok, I'm getting 301 moved permanently. It's got the right URL and everything (I switched from specifying the entire path to just a local one because using the entire path has caused me trouble in the past). – Pinpickle Jan 08 '12 at 01:53
0

** Editing because we've learned that there is a 301 Redirect code being returned by the server **

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

301 Redirects lose contents of the POST. So jQuery is sending it along, but the server redirects it to the right location of the script without the POST data. You need to figure out where the right location of the script is and specify that in your jquery call to avoid the redirect.

Julien Palard
  • 8,736
  • 2
  • 37
  • 44
yycroman
  • 7,511
  • 1
  • 19
  • 21
  • I tried doing it that way, but I already it was going to the right path and the browser was sending the variables (Firebug told me so). Didn't change anything, POST variables still not going through to my PHP script. – Pinpickle Jan 08 '12 at 02:20
  • A 301 means the server knows where the 'new' location of the script is. See the "Response" tab in Firebug Net console. It should give you the URL the server is trying to redirect to. You might be doing your print_r in the wrong place. If the function(data) call is getting executed, it means there is a PHP script SOMEWHERE that is sending the right data back to the browser (otherwise your alert would never pop up). – yycroman Jan 08 '12 at 02:22
  • The response just has the script returned by the call, with the blank array printed by PHP. There's no URL. – Pinpickle Jan 08 '12 at 02:25
  • [http://dev-tricks.net/post-data-lost-on-301-moved-permanently] <-- 301 redirects lose contents of the POST. So jQuery is sending it along, but the server redirects it to the right location of the script without the POST data. you need to figure out where the right location of the script is and specify that in your jquery call. – yycroman Jan 08 '12 at 02:31
-1

You don't have method="POST" in your form.

Aleksandar Vucetic
  • 14,715
  • 9
  • 53
  • 56
  • It doesn't matter because @pinpickle is using jQuery `.post`. This intercepts the form submission, processes a post request, then the `return false;` actually cancels the default behavior (form submission in this case). – Brombomb Jan 08 '12 at 01:37
  • I had the method in before but I took it out for some reason. It was messing something up somehow - can't remember exactly what. And as @Brombom said, I don't need it anyway. – Pinpickle Jan 08 '12 at 01:40
  • good point. Can you give us then what $(this).serialize() shows? – Aleksandar Vucetic Jan 08 '12 at 01:43
  • @vucetica I put "dsa" in every field username=dsa&password=dsa&password2=das&screenname=dsa&address=dsa&usergroup=0 – Pinpickle Jan 08 '12 at 01:44