views:

113

answers:

4

Hi Everyone,

I'm basically just trying to submit a form through AJAX and want to show the return of the processing PHP page. The PHP page just validates the input and shows either the errors that occured, or a thank you message to the user. Normally I wouldn't use ajax for it, but in this case the customers setup requires me to.

As simple as that may sound, I can't get it to work.

The jquery (1.4.2) code I'm using:

$(document).ready(function() {
    $('#subscription').submit( function() {
        var inputdata = $('#subscription').serialize();
        alert(inputdata);
        $.post("process_form.php", inputdata, function(data) {alert(data);} );
    });
});

Firebug just shows:

POST process_form - status: aborted

Another curious observation is that the success handler is actually called, but no responseText is displayed. My apache logs show a 304 http message for the process form, after which it shows another line with statuscode 200 for the form, as if it was redirected back there, whether that is normal AJAX behaviour I don't know.

W3C states here:

If the origin of the URL conveyed by the Location header is same origin with the XMLHttpRequest origin and the redirect does not violate infinite loop precautions, transparently follow the redirect while observing the same-origin request event rules. Otherwise, this is a network error.

But I don't think my request violates any of those.

Anyway, long story short: I'm stuck.

Any help would be very much appreciated and off course I'd be happy to supply any further needed information.

Thank you very much for your time.
All the best,
-G

A: 

At a glance, try removing the curly brackets from the $.post() function.

[edit]

$.post("process_form.php", inputdata, function(data) {alert(data);});
Dickie
Thanks for your reply, the curly brackets around the post I usually don't use, I accidentally left them in whilst changing back from $.ajax.
G-M
A: 

Try returning false at the end of your submit handler. I'm not sure of the precise sequence of things that happen, but if you don't return false you won't prevent the form's default action. So, the form will submit the normal way, and XHR will crash into it causing a large explosion.

karim79
Thanks a lot for your reply, adding this to the submit() block did change something. The request now succeeds, and Firebug shows "status 302 found", but still no responseText is given. I will test a few things to see if I can make it work now. Cheers!
G-M
Update: It works now, something in my PHP fired the 302 but I knoew that could happen. Thanks so much for your help :)
G-M
A: 

According to the jQuery-docs, jQuery.post should be called like this:

$.post("process_form.php", inputdata, function(data) {
    alert(data);
});

If the form has an action-attribute, you probably want to disable the default redirect related to a form submission. To achieve this you can use the following code:

$('#subscription').submit(function(event) {
    event.preventDefault();
    // additional code
});
elusive
Thanks for your reply, the curly brackets around the post I usually don't use, I accidentally left them in whilst changing back from $.ajax. Adding event.preventDefault(); stopped the entire ajax-call from working.
G-M
I am not sure how this could avoid ajax requests. Did you forget to add the event-variable as a function parameter? Did Firebug report any JavaScript erros? This would explain why the request did not fire.
elusive
A: 

Damm, beaten to the answer!

The reason why you were getting a "process_form.php" aborted message in Firebug was from your page refreshing itself before the "process_form.php" could reply to the original query, hence it was aborted! If there's no action in the FORM statement, the page assumes a refresh.

For my pennies worth, I try and avoid using jQuery to event handles such as POST and GET, since normally the page then tries a refresh (since no action was declared in the form statement).

I use the following code for the client side, with the PHP returning a formatted XML document.

<body>
 <form id="subscription">
  <input id="1" value="1">
  <input id="2" value="2">
  <input id="3" value="3">
  <input id="4" value="4">
  <input type="button" value="SUBMIT" onclick="submitForm()">
 </form>
 <script type="text/javascript"> 

  $(document).ready(function() {

  });

  function submitForm() {
   $.post("./process_form.php", $('#subscription').serialize(),
    function(xml) {
     alert($("SUCCESS",xml).text());
   }, "xml");
  };
 </script>
</body>

Server side PHP:

<?php
 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
 header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
 header("Cache-Control: no-cache, must-revalidate" );
 header("Pragma: no-cache" );
 header("Content-Type: text/xml; charset=utf-8");

 $XML = '<?xml version="1.0"?>';
 $XML .= "<ROOT>";
 $XML .= "<SUCCESS>1</SUCCESS>";
 $XML .= "</ROOT>";

 echo $XML;
?>

This allows for validation at the server side easily and for the validated answer to be returned easily. (For example: validating against a database, return with SUCCESS=0 and the reason why REASON=Not in database.)

Jim Grant