Skip to main content

How to make a delete confirmation that need a minimum of 1 record to be deleted?



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;}

}




Comments

  1. Your logic is a bit off:

    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;
    }
    }
    // 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/

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?