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
!:(
5 Answers
Reset to default 11You 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.