user9475796
user9475796

Reputation:

How ask form submit confirmation with jQuery in WordPress?

I'm working on a WordPress site and I need to ask confirmation for user deletion.

When the button is clicked no confirmation asked and the form is always submitted. I tried with a submit event and event.preventDefault method but it doesn't work. Which is the most correct and working method to achieve this?

<button type="submit" id="delete_patient" name="delete_patient" form="add_patient_form" formmethod="post" formaction="<?=parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH )?>" class="btn btn-outline-danger"><?=__( 'Elimina paziente', 'halluxvalgus' )?></button>
jQuery(document).ready(function() {  
  jQuery("#delete_patient").on("click", function() {
    return confirm("<?=__( 'Sei sicuro?', 'halluxvalgus' )?>"); 
  });

Upvotes: 1

Views: 435

Answers (2)

Googlian
Googlian

Reputation: 6713

Use e.preventDefault() to override the default method.

jQuery(document).ready(function() {  

  jQuery("#delete_patient").on("click", function(e) {
    e.preventDefault();
    var r = confirm("<?=__( 'Sei sicuro?', 'halluxvalgus' )?>");
    if (r == true) {

       // "You pressed OK!";

    } else {
       // "You pressed Cancel!";

    }
  });

Upvotes: 1

Meiji
Meiji

Reputation: 107

On button click trigger this code

function confirmDelete(){
    var x = confirm('Your text here');
    if(x){
       return true;
    }else{
       return false;
    }
}

And your delete button must look like this

 <button onClick="return confirmDelete()">Delete</button>

On clicking that button an alert will come up either to proceed or not, before executing the delete code.

Upvotes: 0

Related Questions