jQuery not sending POST data

10,202

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.

Share:
10,202
Pinpickle
Author by

Pinpickle

Updated on June 05, 2022

Comments

  • Pinpickle
    Pinpickle almost 2 years

    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?

  • Brombomb
    Brombomb over 12 years
    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).
  • Pinpickle
    Pinpickle over 12 years
    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
    Pinpickle over 12 years
    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.
  • Aleksandar Vucetic
    Aleksandar Vucetic over 12 years
    good point. Can you give us then what $(this).serialize() shows?
  • Pinpickle
    Pinpickle over 12 years
    @vucetica I put "dsa" in every field username=dsa&password=dsa&password2=das&screenname=dsa&addre‌​ss=dsa&usergroup=0
  • Ghost-Man
    Ghost-Man over 12 years
    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 "localhost/pages/"+ fullpath, and in my answer i tried to point out why print_r($_POST) prints an empty array.
  • Brombomb
    Brombomb over 12 years
    Thanks for teaching me how to doa post on jsfiddle. I tried converting this to a fiddle, but didn't know how :)
  • Pinpickle
    Pinpickle over 12 years
    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
    Pinpickle over 12 years
    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
    Pinpickle over 12 years
    Unless I'm missing something - am I missing something in your jsfiddle example?
  • copy
    copy over 12 years
    @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.
  • Pinpickle
    Pinpickle over 12 years
    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
    Pinpickle over 12 years
    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.
  • yycroman
    yycroman over 12 years
    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).
  • Pinpickle
    Pinpickle over 12 years
    The response just has the script returned by the call, with the blank array printed by PHP. There's no URL.
  • Pinpickle
    Pinpickle over 12 years
    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!
  • yycroman
    yycroman over 12 years
    [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.
  • Pinpickle
    Pinpickle over 12 years
    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!