Jump to content

checkboxes validation


calmchess

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.