Like the title says, I have a problem with checkbox validation.
So, I have this line in my php file which displays users.
<a href='#' onclick='if(confirm("Are you sure?"))checkbox.submit()' class='bt_red'><span class='bt_red_lft'></span><strong>Delete items</strong><span class='bt_red_r'></span></a>
in my delete_items.php file I wrote the following :
$checkbox = $_REQUEST['checkbox'];
if(!isset($checkbox)) {
//diplay an error message :"No user selected"
} else {
if(isset($checkbox)) {
// deleting users script
}
}
In the 2 cases, when clicking on the “delete items” , I get the javascript confirmation message, which I don’t want to display when no checkbox is set (the if(!isset($checkbox)) statement).
thank you for help.
You would want to make the javascript code inside your links onclick statement aware of how many checkboxes have been clicked. This is rather easy. If you use Jquery you can just query them like this:
This just evaluates to how many checkboxes are checked anywhere on your page, so you would have to narrow down the query with a parent element or class or something.
When you have the number of checked checkboxes, its just a matter of some AND and OR like this:
The first expression evaluates to true if there are checked checkboxes, so the second expression gets evaluated, otherwise not.
hope this helps
edit: you could also use if instead of or. maybe that’s a little bit more familiar