I need help on making a delete confirmation that need a minimum of 1 record to be deleted. I'm still confused on making it. I think there's something wrong in my javascript code. Any help would much be appreciated. Thanks
here's the php code:
enter code here
<script src="javascript.js" type="text/javascript"></script>
<?php
echo"<form method=POST action='action.php?act=delete'>
<input type=checkbox name='checkbox[]' value='1'>1
<input type=checkbox name='checkbox[]' value='2'>2
<input type=checkbox name='checkbox[]' value='3'>3
<input type=submit value=Delete onClick='return del_confirm();'></form>";
?>
here's the javascript code:
enter code here
function del_confirm()
{
var msg=confirm('Are you sure?');
var c=document.getElementsByName('checkbox[]');
if(msg)
{
for(i=0;i<c.length;i++)
{
if(c[i].checked)
{
return true;
}
else
{
alert("Select minimum of 1 record to be deleted!");
return false;
}
}
}
else
{return false;}
}
Your logic is a bit off:
ReplyDeletefunction del_confirm() {
var msg = confirm('Are you sure?');
var c = document.getElementsByName('checkbox[]');
if(msg) {
for(i = 0; i < c.length; i++) {
if(c[i].checked) {
return true;
}
}
// This has to be outside the for loop,
// that way it only gets here if every box is not checked
alert("Select minimum of 1 record to be deleted!");
return false;
} else {
return false;
}
}
You had the alert inside the for loop so the first unchecked box returned false for the function.
Example Fiddle: http://jsfiddle.net/FfkvW/