Search code examples
jquerydialogconfirmation

Jquery dialog confirmation, confirm form post


I am trying to display jquery's dialog confirmation before submitting a form. But I can't get it to popup up only when the form is submitted this is the code:

$(function remove() {                           
    $("#dialog-confirm").dialog({
        resizable: false,
        height:200,
        modal: true,
        buttons: {
            'Delete campaign': function() {
                return true ;
                $(this).dialog('close');
            },
            Cancel: function() {
                return false;
                $(this).dialog('close');
            }
        }
    });
});

Dialog confirmation content

  <div id="dialog-confirm" title="Delete ?" style="display:none;">
    <p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>This will be permanently deleted and cannot be recovered. Are you sure?</p>
</div>

Form submit content

<form style="display: inline;" action="remove.php" method="post"" onsubmit="return remove()">

Solution

  • the function remove shouldn't be placed in a $(...);, as $(function(){}) is something that will execute automatically when the document is loaded, just move the function to be defined plainly at the root. Also I would recommend from using inline callback; set an id on the form and use following instead:

    i.e.

    function remove() {
      ...
    }
    $(function(){
      $('#formid').submit(remove);
      // normal initializing code here, which is executed when document is ready
    })
    

    or you could as well define the callback directly:

    $(function(){
      $('#formid').submit(function(){
        // same code as in remove function above
      });
      // normal initializing code here, which is executed when document is ready
    })