display an alert message on successfull submission of a form
I have the following jquery method in my javascript file which calls .submit()
method to submit a form:
var actionOfForm = $("#custom_targeting_param_form").attr('action');
actionOfForm = actionOfForm.replace('#export','');
actionOfForm = actionOfForm+'#export';
$("#custom_targe开发者_开发百科ting_param_form").attr('action',actionOfForm);
try{
$("#custom_targeting_param_form").submit();
$("#showOrExportCustomTargetingReport").val('saveReport');
alert('Report Saved Successfully');
}catch(e){
//alert("ERROR OCCURRED :: "+e);
}
Now what I want is to alert a message "Your form has been submitted successfully" on the successful submission of "custom_targeting_param_form".
As I have used the alert here, it happens prior to the actual submission. Please help.
Submit the form asynchronously in order to show a confirmation alert or render a startup js from the server side which will alert the confirmation message.
Code to submit the form asynchronously
$(function(){
$("#custom_targeting_param_form").submit(function(e){
e.preventDefault();//to stop the default form submit
var actionOfForm = $("#custom_targeting_param_form").attr('action');
actionOfForm = actionOfForm.replace('#export','');
actionOfForm = actionOfForm+'#export';
$("#showOrExportCustomTargetingReport").val('saveReport');
$.ajax({
url: actionOfForm,
type: $(this).attr("method"),
data: $(this).serialize(),
success: function(){
alert('Report Saved Successfully');
},
error: function(){
alert('Report Saving Failed. Please try again later');
}
});
});
Use jQuery ajax() method with a callback.
精彩评论