When you submit a form, the next page displayed is normally the "success" or "thanks" page. This is the easiest approach, and usually it is a perfectly reasonable approach. This is what you will get by default when you set up a form as described in Creating Forms for User Submissions.
There are times, however, when you want to submit the form but stay on the same page. For example, you might have a small contact form in the sidebar of a page, and want to continue to display that page after the form is submitted.
You can do this by using an Ajax form submission. The form is submitted using JavaScript, rather than the normal form submit action, and the confirmation or error message is displayed by inserting the content using JavaScript.
There's a few steps to converting a form submission to use an Ajax submit:
Note: because of the JavaScript security model, you cannot reliably perform Ajax form submits to a domain other than the one that is hosting the form. So you cannot use this technique with forms that submit to another service, such as an email marketing provider.
Here's an example that illustrates the first three items above:
<div id="form_success"></div>
<div id="form_errors"></div>
<div id="form_wrapper">
<form ...>
(normal form code goes here)
<input type='hidden' name='contact[ajax]' value='json' />
</form>
</div>
The only change we've made to the form itself is to add a hidden form element with the name contact[ajax]
whose value is set to json
. This tells the Webvanta system that you want to perform an Ajax submit, and that you'd like the results delivered in json format. Make sure to adjust the name attribute's prefix of contact
to be what-ever name prefix your form actually uses.
Now you just need a little JavaScript to handle the form processing:
<script type="text/javascript">
$(document).ready(function(){
$("form").submit(function() {
$("input[type='submit']").attr("disabled","disabled");
$.post($("form").attr("action"),
$("form").serialize(),
function(data, textStatus){
if (data.status == 'ok') {
$("#form_wrapper").hide();
$("#form_success").text("Thank you for your submission");
} else {
$("input[type='submit']").removeAttr("disabled");
if (data.errors.length > 0) {
$("#form_errors").html("<p>The following errors occurred:</p><ul>" +
$.each(data.errors, function(){
"<li>" + this + "</li>";
}) + "</ul>" );
} else {
$("#form_errors").text("Something went wrong:" + textStatus);
}
}
},
'json');
return false;
})
});
</script>
Here's a summary of what this code is doing:
post
function performs the ajax submit. The parameters are:
Most of the complexity is in the function that processes the response—in particular, in dealing with any errors. Webvanta returns two objects:
data
object, which contains:
data.status
) that will contain the string "ok" if all went well.data.errors
) that explain any validation errors.textStatus
) that explains what went wrong, if there was some sort of communication or server error.Now that we understand these two objects, the actions performed by the result-processing function are clear. If the status is "ok", meaning that the form was successfully submitted:
div
.You should be using JavaScript validation on the form, so any validation errors will be caught before the form is submitted. But if something slips through and the server rejects the post, you need to be prepared.
If the submit fails, things are a little more complicated. The code:
data.errors
array), display them. In the example here, we've formatted them in an unordered list.data.errors
is empty), then there must have been some sort of communication or server error, so display the textStatus string.You can easily adapt any of your own forms to use this Ajax submit approach. Just remember to follow each of the steps outlined in the beginning of this article, and add the JavaScript code above to the head section of your page.
If you have only one form on the page, and you name your div
s as in our example, you should be able to use the JavaScript code we've shown here without modification. If you have multiple forms, change each instance of $("form")
to $("#form_id")
, where form_id
is the ID of the form you want to work with.