This is my first question on StackOverflow and I've already visited every single question about jQuery Dialog & Forms, but still have an issue.
I'm trying to implement a jQuery Confirmation Dialog on my form. The window actually appears without issues, but when I click "Confirm", the form isn't submitted...
This is the Javascript :
// Form validation
$(function(){
var currentForm;
$("#dialog-confirm").dialog({
autoOpen: false,
resizable: false,
modal: true,
buttons: {
'Confirm': function(e) {
currentForm.submit();
return true;
},
Cancel: function() {
$(this).dialog('close');
return false;
}
}
});
$("#news-listing").submit(function(e){
currentForm=$(this);
if($("#dialog-confirm").dialog("open"))
{
return false;
}
});
});
And there is the HTML
<div id="dialog-confirm" title="Empty the recycle bin ?">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin: 20px 10px 20px 10px;"></span>These items will be permanently deleted and cannot be recovered. Are you sure?</p>
</div>
<form action="news/action/delete" id="news-listing">
<table id="users-listing">
<tr>
<th class="id">ID</th>
<th class="title">Title</th>
<th class="url">URL</th>
<th class="modify">Modify</th>
<th class="checkcell">Delete</th>
</tr>
<tr class='alt'>
<td class='id'>".$news['news_id']."</td>";
<td class='title'>".$news['news_title']."</td>";
<td>".$this->News->get_url($news['news_id'])."</td>";
<td class='modify'><a href="modify/1">Modify</a></td>;
<td><input type="checkbox" name="delete[]" /></td>
</tr>
</table>
<input type="submit" name="submit" />
</form>
Thank you in advance for your help !
EDIT : Here is my new code, the HTML does not change. Now I have the dialog, but confirm button doesn't submit, cancel button works well.
$(function() {
// Get the current form object
var currentForm = $("#news-listing");
// Initialize dialog
$("#dialog-confirm").dialog({
autoOpen: false,
resizable: false,
modal: true,
buttons: {
'Confirm': function() {
currentForm.submit();
return true;
},
Cancel: function() {
$(this).dialog('close');
return false;
}
}
});
// Validate the form
$("#news-listing").validate({
rules:{
"delete[]":"required"
},
submitHandler: function(e){
$("#dialog-confirm").dialog("open");
}
});
});
The problem is that currentForm
is undefined
when this line is executed:
currentForm.submit();
An error will be thrown because undefined
does not have a submit
method!
You attempt to set the value of currentForm
in the submit
event handler of the form itself, so you are suffering from something of a chicken and egg problem. You need to assign the form element to currentForm
outside of the event handler:
$(function() {
var currentForm = $("#news-listing");
$("#dialog-confirm").dialog({
//Dialog options...
//currentForm.submit() will now work here
});
$("#news-listing").submit(function(e) {
//Event handler...
//This will be executed when currentForm.submit() is called
});
});