最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - javascript delete confirmation before deleting - Stack Overflow

programmeradmin1浏览0评论

I am displaying a JavaScript confirmation dialog that asks if the user is sure they wish to delete the record. However, even when the user clicks "yes" the query is still executed and the record is still deleted what can be the problem here is the code:

<script>
    function deleletconfig() {
        var del=confirm("Are you sure you want to delete this record?");
        if (del==true){
            alert ("record deleted")
        } else {
            alert("Record Not Deleted")
        }
    }
</script>

So even if I click cancel the query/record gets deleted. How can I stop this from happening what am I doing wrong? Still a newbie at JS!:(

I am displaying a JavaScript confirmation dialog that asks if the user is sure they wish to delete the record. However, even when the user clicks "yes" the query is still executed and the record is still deleted what can be the problem here is the code:

<script>
    function deleletconfig() {
        var del=confirm("Are you sure you want to delete this record?");
        if (del==true){
            alert ("record deleted")
        } else {
            alert("Record Not Deleted")
        }
    }
</script>

So even if I click cancel the query/record gets deleted. How can I stop this from happening what am I doing wrong? Still a newbie at JS!:(

Share Improve this question edited Dec 6, 2014 at 17:51 Reeno 5,70511 gold badges39 silver badges51 bronze badges asked Apr 22, 2013 at 17:26 user2288273user2288273 151 gold badge1 silver badge6 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 11

You must use the return value of the confirm dialog:

echo"<form method = \"post\" action =\"change.php?change=$productid\">";
echo// form fields here!...
echo"<input type=\"submit\" name = \"delete\" value=\"Delete\" onclick=\"return deleletconfig()\" />";

if (isset($_POST['delete'])){  //delete clicked
//get variables here
//run query delete record from xyz where id=$id


}

<script>
    function deleletconfig(){

    var del=confirm("Are you sure you want to delete this record?");
    if (del==true){
       alert ("record deleted")
    }else{
        alert("Record Not Deleted")
    }
    return del;
    }
</script>

See the changes in "onclick" and "deleletconfig".

Create a function and call it:

<script type="text/javascript">
    function confirmation() {
      return confirm('Are you sure you want to do this?');
    }
</script>

<a href="#" onclick="return confirmation()">Delete</a>

Just copy and paste

it gives an alert box to confirm whatever you want your link to do.

Alert returns true or false based on selected answer, and then will either do what you've set your link to do or will not do what your link is supposed to do.

<td>
   <a class="delete_button" href="#">Delete</a>
</td>

<script type="text/javascript">
    $('.delete_button').click(function(e){
        var result = confirm("Are you sure you want to delete this user?");
        if(!result) {
            e.preventDefault();
        }
    });
</script>

Instead of

if (del==true){

do

if(del) {

add return = true for deletion, return = false for no deletion.

发布评论

评论列表(0)

  1. 暂无评论