calmchess Posted February 10, 2010 Share Posted February 10, 2010 I have a form with 6 checkboxes i want php to perform validation on the 6 checkboxes so that if 1 is checked it is ok and the script continues to execute but if 2 or more of the checkboxes is checked then an error message is provided and the script stops executing..........what is the best way to code this? Link to comment https://forums.phpfreaks.com/topic/191565-checkboxes-validation/ Share on other sites More sharing options...
alexjb Posted February 10, 2010 Share Posted February 10, 2010 Could create an array, i.e. <?php if(array_key_exists('act', $_GET) && $_GET['act'] == 'submit') { if(!array_key_exists('test', $_POST) || ! is_array($_POST['test']) || count($_POST['test']) == 0) { // No checkboxes were ticked die("No checkboxes selected."); } if(count($_POST['test']) > 1) { // More than one are checked die("You may only select one textbox"); } // Only one is selected, continuing. echo "Continuing..."; exit; } ?> <form action="?act=submit" method="post"> <input type="checkbox" name="test[]" value="Bleh1" />Bleh1 <br /> <input type="checkbox" name="test[]" value="Bleh2" />Bleh2 <br /> <input type="checkbox" name="test[]" value="Bleh3" />Bleh3 <br /> <input type="checkbox" name="test[]" value="Bleh4" />Bleh4 <br /> <input type="checkbox" name="test[]" value="Bleh5" />Bleh5 <br /> <input type="checkbox" name="test[]" value="Bleh6" />Bleh6 <br /> <input type="submit" value="Submit"> </form> Something like that should work. Link to comment https://forums.phpfreaks.com/topic/191565-checkboxes-validation/#findComment-1009828 Share on other sites More sharing options...
calmchess Posted February 10, 2010 Author Share Posted February 10, 2010 Thanks for the code I will fix it up an put it in my website......my only other question is how do i get the value of the checkbox when it continues? Link to comment https://forums.phpfreaks.com/topic/191565-checkboxes-validation/#findComment-1009836 Share on other sites More sharing options...
alexjb Posted February 10, 2010 Share Posted February 10, 2010 In the case of my example, the value would be given by: $_POST['test'][0]; Link to comment https://forums.phpfreaks.com/topic/191565-checkboxes-validation/#findComment-1009841 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.