user3011784 user3011784 - 1 month ago 19
Javascript Question

jquery AjaxSubmit function how to get result from the form page to display

I'm trying to understand how to use ajaxSubmit() properly and I'm having some difficulties. I want to submit my form with ajax and after change the container value to the result echo by the form. I have an HTML form

<form action="contact.php" method="post" id="contact_form" >
<div id="form-box">
<input type="text" name="name" maxlength="40" >
<input type="submit" name="contact" value="SUBMIT" />
</div>
</form>


And then in JS I use the following code to submit the form to
contact.php


$(document).ready(function() {
var options = {
success: showResponse
};

$('#contact_form').submit(function() {
$(this).ajaxSubmit(options);
return false;
});

});

function showResponse(responseText) {
$("#form-box").html("Thank you message");
}


Now, the
#form-box
changes with my "Thank you message" ... but how do I grab what is echo from contact.php after the script is executed?

I'm not sure how to do this and make it work.

Answer

It would be included in the responseText, assuming your PHP file is actually outputting something.

Try updating your function to

function showResponse(responseText)  { 
 $("#form-box").html("Thank you message"+responseText); 
}