Reputation: 17269
In HTML:
<form id="deleteMessages" name="deleteMessages" action="/deleteInboxMessage" method="post">
<!--some input elements here-->
<input type="submit" id="delete" value="delete"/>
</form>
I use jquery to submit the form:
$('#delete').bind('click', function(e){
e.preventDefault();
//some logic here
$('#deleteMessages').submit();
});
It works in Firefox, IE, Chrome, Opera but not in Safari (in Windows). What happened in Safari is that it opens a new tab with the same content as the page. What's the problem? Why does Safari open a new tab?
I also tried button instead of submit but same problem.
Upvotes: 2
Views: 7323
Reputation: 4792
I experienced this problem also, and could not find a suitable fix.
Instead, I did this:
First, Change your submit to a button.
<input type="button" value="Submit" class="btn loading">
Import Jquery Validate, this will ensure any "required" fields get validated before submitting:
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/jquery.validate.min.js"></script>
Then add this code. It will work with any button that you set class="btn loading"
$(function(){
$(".btn.loading").click(function(e)
{
var btn = $(this);
if (btn.closest('form').valid()) {
btn.html('<i class="fa fa-refresh fa-spin"></i> Uploading...Please wait').prop("disabled",true);
setTimeout(function() {
btn.closest('form').submit();
},100);
}
});
});
The time out is necessary, otherwise the moment you call .submit() on Safari, everything else is delayed in queue.
Works in Safari, Chrome and Firefox on Mac (Probably on Windows too).
Edit: Also tested and working in IE9 on Windows 7.
Also, don't forget that doing this will remove $_POST['SubmitButtonName']
from your list of posted variables. So if you're doing checks against the $_POST['buttonName']
be sure to change your logic or add a hidden field.
Upvotes: 1
Reputation: 177860
Do NOT submit a form in a submit button click or a form submission event
Use
$('#deleteMessages').on('submit', function(e){
e.preventDefault();
//some logic here
some AJAX here!
});
OR
$('#deleteMessages').on('submit', function(e){
//some logic here
});
Upvotes: 1