Jump to content

form checkboxes help


boontoony

Recommended Posts

Hi,

 

I am making a form and have 3 check boxes, in which one "has" to be selected. So if one is already selected, then I select another, the other is then automatically unselected. How would I do this? Also, how do I validate this?

 

Any help appreciated. Thanks.

 

<form name="theForm" method="POST" action="send.php">

  Name:

  <input name="name" type="text" size="40" />

  <br>

  E-Mail:

  <input name="email" type="text" size="40" />

  <br>

  Type:

  <input name="check[]1" type="checkbox" value="1" />

  Test 1

  <input name="check[]2" type="checkbox" value="2" />

  Test 2

  <input name="check[]3" type="checkbox" value="3" />

Test 3<br>

  Please click one.<br>

  <input name="submit" type="submit" value="Submit">

</form>

 

<?php
if(isset($_POST['submit'])) {
$subject = "Enquiry";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$header = "From: ". $name_field . " <" . $email_field . ">";

foreach($_POST['check'] as $value) {
	$check_msg .= "Type: $value\n";
}	


$body = "DETAILS\n Name: $name_field\n E-Mail: $email_field\n $check_msg\n "; 

echo "<h2>Your e-mail has been sent!</h2>";
mail($to, $subject, $body, $header);

} else {
echo "<h2>Sorry, there was an ERROR. Please go back.</h2>";
}
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/147661-form-checkboxes-help/
Share on other sites

Change the input type from checkbox to 'radio'. Each input box should have the same name but each should have a different value.

 

For example, if you name the 3 input boxes to 'test' and give them the values, 1, 2, 3 respectively, when you use $_GET['test'], either 1, 2 or 3 will be returned.

 

This isn't a PHP issue but I hope it makes sense anyway.

Link to comment
https://forums.phpfreaks.com/topic/147661-form-checkboxes-help/#findComment-775157
Share on other sites

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.