javascript - AJAX call working in Chrome, but not in Safari, Firefox or Internet Explorer
I have a single page with multiple forms, submitting each of them with an AJAX call to the same PHP controller. In Chrome, when I submit the form, I get a beforeSend message, and then a success call. In Safari, IE and Firefox, it instead refreshes the page, thus seemingly going right past the ajax call that should keep it from refreshing.
The forms are of this type:
<form method="post" class = "multi-form teacher_account_form" id="personal_form" name="personal_form">
<div class = "form-group">
<label for="phone">What is your phone number?*</label>
<input type="text" data-format="+86 ddd dddd dddd" name="phone" id="phone" value="<?=$this_user[0]['phone']?>">
</div>
<div >
<button type="submit" id="personal_submit" name="personal_submit">Update Personal</button>
</div>
</form>
The JS is like this:
var which_button;
$('.account_submit').click(function() {
which_button = $(this).attr('id');
// Create empty jQuery objects -
var $jsOutput = $([]);
var $jsForm = $([]);
// url to submit to
var ajaxURL = "/users/p_teacher_account_work";
// Assign jQuery selector objects
switch (which_button) {
case "work_submit":
$jsOutput = $('#pay_output');
$jsForm = $('#pay_form');
break;
case "edu_submit":
$jsOutput = $('#edu_output');
$jsForm = $('#edu_form');
break;
case "image_submit":
$jsOutput = $('#image_output');
$jsForm = $('#image_form');
break;
case "personal_submit":
$jsOutput = $('#personal_output');
$jsForm = $('#personal_form');
break;
}
// Lock and load the ajax request -
var ajax_options = {
type: 'post',
url: ajaxURL,
beforeSend: function() {
//Display a loading message while waiting for the ajax call to complete
$jsOutput.html("Updating...");
},
success: function(response) {
$jsOutput.html(response);
}
};
$jsForm.ajaxForm( ajax_options );
});
My php files query the database and send back information to a view which is echoed to the same page that the forms sit on.
Answer
Solution:
Try using type="button" instead of type="submit".
As in:
It's hacky, but it works in every browser I've tried. The type="submit" means something special to most browsers.
Edit: Wait...are you using Malsup's "jQuery Form Plugin"?
Answer
Solution:
Two things:
1.) Turn your button type to
button
a la:2.) Change your event handler to call
$jsForm.ajaxSubmit( ajax_options );
instead of$jsForm.ajaxForm( ajax_options );
According to their docs,
$jsForm.ajaxForm( ajax_options );
doesn't actually submit the form. The fact that this was working as you expected in Chrome suggests it's doing something weird behind the scenes when submitting forms.Answer
Solution:
Try this solution for a plain, jQuery option:
Now, you can make the button type="button" or even replace it with an element with no default onclick event action such as a span.
Answer
Solution:
It was not working because of a boostrap formhelpers plugin country picker dropdown http://bootstrapformhelpers.com/country/#jquery-plugins that wasn't loading correctly in the problematic browsers. For what it's worth, here is the code that wound up working (not including the removal of that nefarious plugin):