I have created a checkDelete function to ask the user if he wants to delete the selected entries from a database. The problem is that choosing cancel still deletes those entries.
This is the function:

function checkDelete (){
    var answer = confirm("Are you sure you want to delete the selected entries?");
    if (answer == true) {
        document.forms[0].submit()
    }
    else 
    }
    </script>

thank you.

Recommended Answers

All 9 Replies

It seems that the entries are deleted because the form is submitted to the server even after you click cancel on the confirm box. One of the reason for this could be that the delete button could be of the type submit
e.g.
<input type="submit" name="Delete">
make the type of normal button.
i.e
<input type="button" name="Delete">

It would help if you could post your complete code of the web page related to this functionality

This is the code:

echo "<form action='change.php?modify_type=2' onsubmit='checkDelete()' method='post'>";

foreach($rows as $row){
echo "<input type='checkbox' name='delete_id[]' value='".$row[0]."'>";
}

echo "<input type='Submit' value='Delete'>";

You may want to return false from your function, if you do not want to submit.

I have returned false on the else branch but it is still deleting

Yup as pritaeas suggested, do

echo "<form action='change.php?modify_type=2' onsubmit='return checkDelete()' method='post'>";

And return false from the javascript function when cancel is clicked,

function checkDelete (){
var answer = confirm("Are you sure you want to delete the selected entries?");
if (answer == true) {
   document.forms[0].submit()
}
else {
   return false;
}
}
</script>

Also has to do

onsubmit='return checkDelete()'

I have created a checkDelete function to ask the user if he wants to delete the selected entries from a database. The problem is that choosing cancel still deletes those entries.
This is the function:

function checkDelete (){
    var answer = confirm("Are you sure you want to delete the selected entries?");
    if (answer == true) {
        document.forms[0].submit()
    }
    else 
    }
    </script>

thank you.

just add return from where u r calling this checkDelete() function like parry_kulk as said
something like

function submit()
{
 return checkDelete()
}

function checkDelet()
{


}

thank you very much. it works now.

everybody always wants to do it the hard way echo "<form action='change.php?modify_type=2' onsubmit='return confirm(\"Are you sure you want to delete the selected entries?\");' method='post'>"; keep it small

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.