ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Web Contact Form (Radio Buttons, Checkboxes)

Updated on January 17, 2014

Radio Buttons and Checkboxes

Looking on the web you can find numerous examples of simple web contact forms using text boxes and text areas, but the ones dealing with radio buttons and check boxes are hard to come by.

This simple how-to will show how to display, validate and email the form contents from a PHP page to an email box, it will use text boxes, text areas, check boxes and radio buttons.

We are going to use the multi-purpose page approach leaving everything to be done on one page or script, no outside pages or scripts fro validation or messages. Easier to maintain and easier to deploy the script in a real world setting.

No need to be familiar with PHP, but here are the functions that we will be using in the article.

  • isset()
  • empty()
  • foreach()
  • !
  • mail()
  • $_SERVER

The multi-purpose page approach is based on two things concerning the form submittal:

  1. When I don't push the submit button
  2. When I do push the submit button

On step one we use the isset() function to look to see if the Submit button has been set or pushed which would cause it to be set.

<!--this will show the form-->
<?php if(!isset($_POST['Submit'])){
$errMess = 'Welcome to the Form Test';//you change this message;
$aClass = 'norm';//look at the css
?>
<h3><?=$errMess;?></h3>
<form name="form1" id="form1" action="<?=$_SERVER['PHP_SELF'];?>" method="post">

<!-- rest of code -->

First line:

if(!isset($_POST['Submit']) using the isset function to look for the Submit button variable, we know that when the form first loads it will stop from self submitting, because the button was not pushed.

This is the variable in raw form $_POST['Submit'] that we are looking for using the isset function.

We could rewrite that and assign it to another variable i.e. $submit = $_POST['Submit'], but that is beyond the scope of the tutorial.

This is the form code:

<form name="form1" id="form1" action="<?=$_SERVER['PHP_SELF'];?>" method="post">
<table>
<tr>
<td>Email</td><td><input class="<?=$aClass;?>" id="email" name="email" type="text" value="<?=$_POST['email'];?>"/></td>
</tr>
<tr>
<td>Is the sky blue</td>
<td>
<ul class="<?=$aClass;?>">
<li><input name="sky" type="radio" value="Yes" /> Yes</li>
<li><input name="sky" type="radio" value="No" /> No</li>
</ul>
</td>
</tr>
<tr>
<td>Things you like about the sky</td>
<td>
<ul class="<?=$aClass;?>">
<li><input name="likeSky[]" type="checkbox" value="stars" /> Stars</li>
<li><input name="likeSky[]" type="checkbox" value="moon" /> Moon</li>
<li><input name="likeSky[]" type="checkbox" value="clouds" /> Clouds</li>
</ul>
</td>
</tr>
<tr>
<td>Message</td><td><textarea name="comments" rows="5" class="<?=$aClass;?>" id="comments"></textarea></td>
</tr>
<tr>
<td colspan="2"><div align="center"><input id="Submit" name="Submit" type="submit" value="Submit" /></div></td>
</tr>
</table>

As you can see nothing fancy or complicated.

We now go into step two of our multi-purpose form technique, we pushed the submit button, here it is.

}else{
//set up the variables for validation and then processing
$email = $_POST['email'];
$comments = $_POST['comments'];
$sky= $_POST['sky'];
$likeSky = $_POST['likeSky'];

The above just takes the form values and assigns them to a variable easier to deal with in the validation and the processing.

We have to be able to stop the form if we have missing form elements, like the following:

//some simple validation for empty values
if(empty($email) || empty($comments) || empty($sky) || empty($likeSky)){
$errMess = 'Fill out the form!';//you change this message;
$aClass = 'err';//look at the css

The empty() function, just does what it says; looks for nothing, and if it does its job, the script will stop and display our simple form again, along with some CSS values assigned to a PHP variable, that way you can change the look of the form for errors without re-writing it.

The next step is secondary step built into step two, this will happen when the form is filled out and ready to be emailed:

$to = $email;//you can change this line and put in your own email i.e. "whateverMyEmailIs@yoyo.com"
$subject = "$email has got a Question for You!";
$message = "Is the sky is blue?\r\n";
$message.="$sky\r\n\n";
$message.="I love this part of the sky:\r\n";
foreach ($likeSky as $likeSky){
$message.="$likeSky\n";
}
$message.= "\n\n$comments";
//end message, begin headers
$headers = "From: $email\r\n";
$headers.= "Reply-To: $email\r\n";
$headers.= "X-Mailer: PHP/" . phpversion();
$helloWorld = true;

Now in bold we have hi-lighted the foreach loop to look though the form values that are in the checkbox array, we use this because on the form we display the checkboxed values are like so.


<li><input name="likeSky[ ]" type="checkbox" value="stars" /> Stars</li>
<li><input name="likeSky[ ]" type="checkbox" value="moon" /> Moon</li>
<li><input name="likeSky[ ]" type="checkbox" value="clouds" /> Clouds</li>

Notice the names we gave the checkboxes are all the same names, and we also named all the radio buttons the same name:

<li><input name="sky" type="radio" value="Yes" /> Yes</li>
<li><input name="sky" type="radio" value="No" /> No</li>

One difference; we don't need the radio buttons to have multiple values assigned, but we do with the checkboxes, because they are created to have such ability (to carry multiple values).

The brackets [ ] are PHP's way of assigning it (element) to a collection of values i.e. sky collection has stars, moon, clouds, etc, the list could gone on, but with this form we wanted this selection of options for the user.

More could be said and written about arrays but again, the explanation is outside of the scope of this article.

The last function we go over is the mail function:

mail($to,$subject,$message,$headers);

All this does is take take the variables and send them out (email).

In the script we validate whether the function worked or not, (not if the person got the email), plenty can be written on that subject, we accomplish this like so:

if($helloWorld !=true){
$errMess = 'Email Failed!';//you change this message;
$aClass = 'err';//look at the

Earlier in the how-to we see a variable that we assigned the value true to:

$helloWorld = true;

That would only be true if the mail function worked properly, if not an error message will display and the form will show and ready for another try at it.

If the mail function did not fail we simply show a message and of course our handy form ready for another message.

Here is the entire code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Email testo</title>
<style>
body{font-family:Arial, Helvetica, sans-serif;font-size:13px;}
table{width:300px;border:1px solid #cccccc;background-color:#f1f1f1;}
h3{color:#0099FF;}
/*--classes for the form --*/
.norm{font-family:Arial, Helvetica, sans-serif;font-size:11px;list-style:none;}
.err{font-family:Arial, Helvetica, sans-serif;font-size:11px;border:1px solid #FF0000;}
</style>
</head>

<body>
<!--this will show the form-->
<?php if(!isset($_POST['Submit'])){
$errMess = 'Welcome to the Form Test';//you change this message;
$aClass = 'norm';//look at the css
?>
<h3><?=$errMess;?></h3>
<form name="form1" id="form1" action="<?=$_SERVER['PHP_SELF'];?>" method="post">
<table>
<tr>
<td>Email</td><td><input class="<?=$aClass;?>" id="email" name="email" type="text" value="<?=$_POST['email'];?>"/></td>
</tr>
<tr>
<td>Is the sky blue</td>
<td>
<ul class="<?=$aClass;?>">
<li><input name="sky" type="radio" value="Yes" /> Yes</li>
<li><input name="sky" type="radio" value="No" /> No</li>
</ul>
</td>
</tr>
<tr>
<td>Things you like about the sky</td>
<td>
<ul class="<?=$aClass;?>">
<li><input name="likeSky[]" type="checkbox" value="stars" /> Stars</li>
<li><input name="likeSky[]" type="checkbox" value="moon" /> Moon</li>
<li><input name="likeSky[]" type="checkbox" value="clouds" /> Clouds</li>
</ul>
</td>
</tr>
<tr>
<td>Message</td><td><textarea name="comments" rows="5" class="<?=$aClass;?>" id="comments"></textarea></td>
</tr>
<tr>
<td colspan="2"><div align="center"><input id="Submit" name="Submit" type="submit" value="Submit" /></div></td>
</tr>
</table>
</form>
<!--this will do the validation and processing-->
<?php
}else{
//set up the variables for validation and then processing
$email = $_POST['email'];
$comments = $_POST['comments'];
$sky= $_POST['sky'];
$likeSky = $_POST['likeSky'];
//some simple validation for empty values
if(empty($email) || empty($comments) || empty($sky) || empty($likeSky)){
$errMess = 'Fill out the form!';//you change this message;
$aClass = 'err';//look at the css
?>
<h3><?=$errMess;?></h3>
<form name="form1" id="form1" action="<?=$_SERVER['PHP_SELF'];?>" method="post">
<table>
<tr>
<td>Email</td><td><input class="<?=$aClass;?>" id="email" name="email" type="text" value="<?=$_POST['email'];?>"/></td>
</tr>
<tr>
<td>Is the sky blue</td>
<td>
<ul class="<?=$aClass;?>">
<li><input name="sky" type="radio" value="Yes" /> Yes</li>
<li><input name="sky" type="radio" value="No" /> No</li>
</ul>
</td>
</tr>
<tr>
<td>Things you like about the sky</td>
<td>
<ul class="<?=$aClass;?>">
<li><input name="likeSky[]" type="checkbox" value="stars" /> Stars</li>
<li><input name="likeSky[]" type="checkbox" value="moon" /> Moon</li>
<li><input name="likeSky[]" type="checkbox" value="clouds" /> Clouds</li>
</ul>
</td>
</tr>
<tr>
<td>Message</td><td><textarea name="comments" rows="5" class="<?=$aClass;?>" id="comments"></textarea></td>
</tr>
<tr>
<td colspan="2"><div align="center"><input id="Submit" name="Submit" type="submit" value="Submit" /></div></td>
</tr>
</table>
</form
><!--this will end the conditional statement-->
<?
}else{
//if we get past the validation we parse it
//mail the message
$to = $email;//you can change this line and put in your own email i.e. "whateverMyEmailIs@yoyo.com"
$subject = "$email has got a Question for You!";
$message = "Is the sky is blue?\r\n";
$message.="$sky\r\n\n";
$message.="I love this part of the sky:\r\n";
foreach ($likeSky as $likeSky){
$message.="$likeSky\n";
}
$message.= "\n\n$comments";
//end message, begin headers
$headers = "From: $email\r\n";
$headers.= "Reply-To: $email\r\n";
$headers.= "X-Mailer: PHP/" . phpversion();
$helloWorld = true;
//testo baby
//echo $to;
//echo $subject;
//echo $message;
//return;
mail($to,$subject,$message,$headers);
if($helloWorld !=true){
$errMess = 'Email Failed!';//you change this message;
$aClass = 'err';//look at the css
?>
<h3><?=$errMess;?></h3>
<form name="form1" id="form1" action="<?=$_SERVER['PHP_SELF'];?>" method="post">
<table>
<tr>
<td>Email</td><td><input class="<?=$aClass;?>" id="email" name="email" type="text" value="<?=$_POST['email'];?>"/></td>
</tr>
<tr>
<td>Is the sky blue</td>
<td>
<ul class="<?=$aClass;?>">
<li><input name="sky" type="radio" value="Yes" /> Yes</li>
<li><input name="sky" type="radio" value="No" /> No</li>
</ul>
</td>
</tr>
<tr>
<td>Things you like about the sky</td>
<td>
<ul class="<?=$aClass;?>">
<li><input name="likeSky[]" type="checkbox" value="stars" /> Stars</li>
<li><input name="likeSky[]" type="checkbox" value="moon" /> Moon</li>
<li><input name="likeSky[]" type="checkbox" value="clouds" /> Clouds</li>
</ul>
</td>
</tr>
<tr>
<td>Message</td><td><textarea name="comments" rows="5" class="<?=$aClass;?>" id="comments"></textarea></td>
</tr>
<tr>
<td colspan="2"><div align="center"><input id="Submit" name="Submit" type="submit" value="Submit" /></div></td>
</tr>
</table>
</form>
<?php
}else{
$errMess = 'We have received your inquiry and will be in touch shortly!<br/>Thank you!';//you change this message;
$aClass = 'norm';//look at the css
?>
<h3><?=$errMess;?></h3>
<form name="form1" id="form1" action="<?=$_SERVER['PHP_SELF'];?>" method="post">
<table>
<tr>
<td>Email</td><td><input class="<?=$aClass;?>" id="email" name="email" type="text" value="<?=$_POST['email'];?>"/></td>
</tr>
<tr>
<td>Is the sky blue</td>
<td>
<ul class="<?=$aClass;?>">
<li><input name="sky" type="radio" value="Yes" /> Yes</li>
<li><input name="sky" type="radio" value="No" /> No</li>
</ul>
</td>
</tr>
<tr>
<td>Things you like about the sky</td>
<td>
<ul class="<?=$aClass;?>">
<li><input name="likeSky[]" type="checkbox" value="stars" /> Stars</li>
<li><input name="likeSky[]" type="checkbox" value="moon" /> Moon</li>
<li><input name="likeSky[]" type="checkbox" value="clouds" /> Clouds</li>
</ul>
</td>
</tr>
<tr>
<td>Message</td><td><textarea name="comments" rows="5" class="<?=$aClass;?>" id="comments"></textarea></td>
</tr>
<tr>
<td colspan="2"><div align="center"><input id="Submit" name="Submit" type="submit" value="Submit" /></div></td>
</tr>
</table>
</form
><?php
}//end mail if
}//end second if
} ?>
</body>
</html>

We touched on a few key functions and a splash of CSS to give the user some feedback.

That should give you a good primer on how to deal radio buttons and checkboxes in a html form using PHP.

Please do come back and look over my articles to come on ready to use techiques and code concerning PHP, HTML and CSS.

working

This website uses cookies

As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://corp.maven.io/privacy-policy

Show Details
Necessary
HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
LoginThis is necessary to sign in to the HubPages Service.
Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
AkismetThis is used to detect comment spam. (Privacy Policy)
HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
Features
Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
MavenThis supports the Maven widget and search functionality. (Privacy Policy)
Marketing
Google AdSenseThis is an ad network. (Privacy Policy)
Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
Index ExchangeThis is an ad network. (Privacy Policy)
SovrnThis is an ad network. (Privacy Policy)
Facebook AdsThis is an ad network. (Privacy Policy)
Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
AppNexusThis is an ad network. (Privacy Policy)
OpenxThis is an ad network. (Privacy Policy)
Rubicon ProjectThis is an ad network. (Privacy Policy)
TripleLiftThis is an ad network. (Privacy Policy)
Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
Statistics
Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
ClickscoThis is a data management platform studying reader behavior (Privacy Policy)