简体   繁体   中英

Using javascript to ask if the user is sure they want to delete the data

I am trying to run a function that calls some Javascript to ask the question "Are you sure you want to delete this customer?" But my link to the java-script does not seem to work. All I get at the Java-script end is the word remove. It is supposed to ask if they are sure and if not return them back to the page they clicked on. If they are sure, then delete the data.

This script checks to make sure there is no other data connected to the customer's data. If there is they are not allowed to delete it. But if there is nothing attached they can delete it but I want to make sure that's what they want to do.

I think my problem is in my links but not sure.

function checkcustomeruse($custid,$pid,$name){{}
global $db;
$sql = "SELECT COUNT(*) from signings WHERE pid = ? AND custid = ?";
$stmt = $db->prepare($sql);
$stmt->bindParam(1, $pid, PDO::PARAM_INT);
$stmt->bindParam(2, $custid, PDO::PARAM_STR);
$stmt->execute();
$number_of_rows = $stmt->fetchColumn(); 
$number = $number_of_rows;
 if($number == 0)
 {

    echo '<a href="javascript: deleteAlert(custid =' . $custid . ',' . 'name =' . $name . ');">Remove</a>';
 }
 else
 {
  $Message = 'You can not delete this customer because it has signings attached to it.';
       header("Location: viewallcustomer.php?Message=" . urlencode($Message));  
 }

}

This is the javascript

function deleteAlert(custid,name){
    var conBox = confirm("Are you sure you want to delete: " + name);
    if(conBox){
        location.href="formpross.php?processtp=deletecustomers&delete=yes&custid";
    }else{
        return;
    }
}

你可以尝试一下:

function deleteAlert(custid,name){ var conBox = confirm("Are you sure you want to delete: " + name); if(conBox){ location.href="formpross.php"; } return conBox; }

Try echoing out your Javascript function a little differently. The escaped single quotes turn into single quotes around the values we're sending into the javascript function.

 echo '<a href="javascript: deleteAlert( \'' . $custid . '\',\'' . $name . '\');">Remove</a>';

Also, we need to return false if the Javascript function results in cancelling the action.

function deleteAlert(custid,name){
    var conBox = confirm("Are you sure you want to delete: " + name);
    if(conBox){
        location.href="formpross.php?processtp=deletecustomers&delete=yes&custid";
    } else {
        return false;
    }
}

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