简体   繁体   中英

Using jquery / javascript to create a popup confirm box

Here is the extract code of how to make a confim box when delete,

For html part: A link is to trigger JS code , but it will trigger the php code at same time

For JS part: popupbox is triggered

For php part: Process the sql query, it should be ok

The problem are:

  1. I should use js to trigger the php page?But how can i let the php page know that which ListID i want to delete?

  2. What should i put in the html link?

Thank you

html

<a id="delete" href='delete.php?id=$set[ListID]'>Delete</a>

Js

$(function(){
  $("#delete").click(function() {
    $.messager.alert('Warning','The warning message');  
    $.messager.confirm('Confirm','Are you sure you want to delete record?',function(r){  
        if (r){  
            alert('ok');  
        }  
    });  
 });
});

php

//connection db
INSERT  INTO delete_list SELECT * FROM list WHERE ListID=?;    
INSERT  INTO delete_user_list SELECT * FROM user_list WHERE ListID=?;    
INSERT  INTO delete_require_attributes SELECT * FROM require_attributes WHERE ListID='2';    
INSERT  INTO delete_subscriber SELECT * FROM subscriber WHERE ListID=?;    
INSERT  INTO delete_subscriber SELECT * FROM subscriber WHERE ListID=?;    
DELETE FROM list WHERE ListID = '1'

What if i want to include the list name in the popup box eg do you want to delete list A ,where list A is a variable already. The only thing is how can i append it to the popup box

"<tr><td>".$set['ListName']."</td><td>"

I don't know what $.messager does, but I suppose this should work

$(function(){
  $("#delete").click(function(evt) {
    evt.preventDefault();
    var urlscript = this.href;  /* read link url (e.g. delete.php?id=314159) */

    $.messager.alert('Warning','The warning message');  
    $.messager.confirm('Confirm','Are you sure you want to delete record?',function(r){  
        if (r) {                  
           $.ajax(urlscript); /* make ajax call to that url with the right id */    
        }  
    });  
 });
});

and I supposing that your source code is actually showing smthg like

<a id="delete" href='delete.php?id=314159'>Delete</a>

so on the ajax call you're sending that id.

Try this:

if (confirm("Question?")) {
     // IF OK CLICKED
     $.get('delete.php?id=MyID', function(data) {
         alert('List DELETED');
    });
}
$("#delete").click(function(e) {
    e.preventDefault();
    var url = this.href;
    $.messager.confirm('Confirm','Are you sure you want to delete record?',function(r){  
        if (r){  
            location.href = url;
        }  
    });

Let the link trigger the javascript code to show the popup window for delete confirmation. at the same time, you can update value of a hidden field in your page with the selected item id. When you user confirms the deleting by clicking on the "Yes" button in the confirm box. Read the value of the hidden input and then post that to the server (php) page to do the actual deletion from the database.

I wouldnt send the ID as a GET, else people can increment the value and delete your records?

<a class="delete" id="someId" >Delete</a>

$('.delete').click(function() {
  var id = $(this).attr('id');

  $.messager.alert('Warning','The warning message');  
  $.messager.confirm('Confirm','Are you sure you want to delete record?',function(r){  

   if (r) {       
      $.post('delete.php', {id:id}, function(response) {
         //check the status of the reponse
      });  
   });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM