How to Create an HTML/PHP Contact Form – Radio Buttons, Checkboxes, Dropdown Menus

PHP

Welcome back to our contact form tutorial series. If you’re not exactly sure why I’m saying “back”, check this link. This is the third episode and in this tutorial we’re gonna learn how to add different types of inputs to the contact form and process that with PHP. It’ll make your contact form look absolutely gem and the features allow you to obtain some extra info from the user. So, you totally need to know how to do that.
If you followed my two previous contact form tutorials you should have 2 files: contact-form.html and send-mail.php. If not, then you can download them here. Now just open them with your favorite html/php editor.

By the way, if your job involves taking care of a server or you’re just running a website, you really need to take a look at the server maintenance checklist, which allows to ensure that everything is configured properly and working as it’s supposed to.

Radio buttons

The first new input type we are going to add is the radio button. This type of input is used when you want to provide a variety of options for the user, but from that variety a user can select just one.

<p>Select your hosting plan:</p>
<input id="radio_1" type="radio" name="radio_group_1" value="shared">
<label for="radio_1">Shared server</label>

<input id="radio_2" type="radio" name="radio_group_1" value="vps">
<label for="radio_2">Virtual Private Server</label>

<input id="radio_3" type="radio" name="radio_group_1" value="dedicated">
<label for="radio_3">Dedicated Server</label>

Let’s analyze the additional contact form code above. Basically you should be already familiar with all the attributes used, but there are some tricky things.

The first thing you might have noticed is that all the three radio buttons have the same value assigned for the name attribute – radio_group_1. This way we basically combine those 3 radio buttons into one group, so the browser knows that only one radio button can be selected from this specific group.

When the user selects one option from the list, the data of this value attribute is sent to the server. So this attribute is a “must-have” else the server won’t know that any option has been selected.

Now for the PHP code of our contact form. We create a new variable entitled $host_plan that will store the result of the selected radio button.
For example, if we select the second option and submit the contact form, the $host_plan variable’s value will be vps, if the third radio button is selected – then it will be dedicated, and so on if we have more options.

$host_plan = $_POST['radio_group_1'];

CHECKBOX

Checkbox inputs are quite similar to radio buttons, except that a user can select multiple options, not just one.

<p>Additional hosting options:</p>
<input id="check_1" type="checkbox" name="checkbox_group_1[]" value="privacy">
<label for="check_1">Domain Privacy</label>

<input id="check_2" type="checkbox" name="checkbox_group_1[]" value="ssl">
<label for="check_2">SSL certificate</label>

<input id="check_4" type="checkbox" name="checkbox_group_1[]" value="mysql">
<label for="check_3" >Additional MySQL Databases</label>

One thing to note here is how we define the value for the name attribute. I bet you noticed that I added square brackets [ ] at the end, this tells the server to create an array to store the checked values. If we don’t do that, then the server will receive only the value of the last marked checkbox.

To convert an array of checked options into a string, we’ll use the PHP implode function. It basically creates an empty string, then grabs each value from the array, and appends it to the string.

$additional_options = implode(' | ', $_POST['checkbox_group_1']);

Note, the first parameter of the function - ' | ' – it works as a separator.
If we tick all 3 checkboxes and implode the array, the result string would be ” privacy | ssl | mysql ” You can use anything as a separator – a comma, a whitespace or even some text or special symbol.

Dropdown menu

<select name="dropdown">
    <option value="1m">1 month</option>
    <option value="2m">3 month</option>
    <option value="6m">6 month</option>
    <option value="1y">1 year</option>
    <option value="3y">3 years</option>
</select>

The code above creates a dropdown list with 5 options in it. This feature can make any contact form look cool and robust. The syntax is quite simple – the option tags are wrapped inside the opening and closing select tags. The text between the option tags is what we see in the browser, and the value attribute is what the server actually receives. For the select tag, we just need to specify the name attribute. And the PHP code below

$host_period = $_POST['dropdown'];

Final Steps

One last thing to do (which I almost forgot). We should now add some script to our contact form that allows to send the new data to our email. To do that, we can append new variables to the body message. Here’s the last piece of code we need to add to the PHP file

$body_message .= 'User selected the ' . $host_plan . ' hosting plan';
$body_message .= 'Additional options selected: ' . $additional_options;
$body_message .= 'Hosting period: ' . $host_period;

That’s all for today folks. I hope this part of my contact form tutorial was easy enough to follow. If some part was not clear, you can download the final files from this link and investigate the completed code.

Designs with a Built-in Form

Alternatively, you can just use the designs that already have forms so that you get your site off the ground in virtually no time. Check’em out! 🙂

Under Construction Page w/ AJAX contact form

Under Construction Page w/ AJAX contact form

Divine – vCard/Portfolio – With AJAX Contact Form

Divine - vCard/Portfolio - With AJAX Contact Form

Wisdom of Life – HTML Template + PHP Contact Form

Wisdom of Life - HTML Template + PHP Contact Form

iSite – 1 Page Folio + Contact Styling Validation

iSite - 1 Page Folio + Contact Styling Validation

Animated Under Construction – Twitter & Ajax forms

Animated Under Construction - Twitter & Ajax forms

Let me know if you have any questions or issues with your contact form.

Comments

18 responses to “How to Create an HTML/PHP Contact Form – Radio Buttons, Checkboxes, Dropdown Menus”

  1. Kenneth von Rauch Avatar

    Thanks Tobias, that’s really great that you’re going into more detail here, because creating a really simple contact form isn’t that big a deal, but making something what you’re trying to do is really useful. That’s so because it makes your contact form more robust and feature-rich (drop-downs and stuff). Sure thing, too many features are as bad as lack of features, but you seem to fit the bill just right. Keep up the great job, man!

    1. Ron Paul Avatar
      Ron Paul

      I have a drop down created with my list of options to select from the drop down, what i need is a button that will add a new option when i click the button. once clicked i get an option to type the new validated option to add to the list. Also a button to remove a drop down item.

      Good To Go Status:

      <select class="inputtext text ui-corner-all" id="tab_tbltitle_goodtogo" >

      <option value="”>
      Pending Surface
      Pending Minerals
      Pending Both
      Approved
      Denied

      All it needs is the programming to add to the list. can’t quite figure it out.

      1. Kenneth von Rauch Avatar

        Thanks for your comment, Ronald. I am not exactly sure that I get what you want to do. Could you give me a link to a site that has the option functioning as you wish to make yours work? That would make it clear to me. Alternative, please email me. http://webdesy.com/contact/

  2. Ivan Carvba Avatar
    Ivan Carvba

    All I can say is I have tried many tutorials that give no results and your way to explain it is the BEST.

  3. Artyone Avatar
    Artyone

    Thanks for the form tips, very useful.
    How do I make some of the fields as REQUIRED??
    I would like the name and address as required
    Also, is it easy to add a anti spam item to this contact form?
    Thanks

  4. Kenneth von Rauch Avatar

    You can make it required as follows:

    Username:

    As for anti spam, it will require some advanced scripting, but I may be able to help if you explain in more detail how you want it to work.

  5. artemis Avatar
    artemis

    Can more fields be added if you want to create a survey form? This is what I would like to do but I’m having a hard time finding information on how to do this Any help is appreciated. Thanks!

    1. Kenneth von Rauch Avatar

      Yes, sure thing. You can just add as many input text fields as you need. Let me know which exactly text fields you’d like to add and I’l walk you through.

  6. Iftikhar Avatar
    Iftikhar

    I know how to add contact-form.html file to my web page.What should I do with send-mail.php file?

    1. Kenneth von Rauch Avatar

      You need to upload your file to the server as well.

  7. Iftikhar Avatar
    Iftikhar

    Can you help me to add the captcha (please see attached image) to the end of the contact form?Thanks in advance.

  8. MaryMac Avatar
    MaryMac

    Tobias thank you for this as I am a newbie at php forms.

    However,
    I’m getting an error message on my ‘checkbox’ area of the form for the
    ‘implode’ function:

    Notice: Undefined index: checkbox_group_1 in….
    Warning: implode() [function.implode]: Invalid arguments passed in….

    I copied your code… but I may be missing something. Any advice would be GREATLY appreciated!
    Thanks, Mary~

    1. Kenneth von Rauch Avatar

      Please send me your file and we’ll check them for errors.

  9. Roger Mercier Avatar
    Roger Mercier

    When I click on Send in my form, it just tries to display the contact.php file:

    Form:

    Your name

    Your e-mail

    Phone Number

    Are you a new Dimensions user:
    Yes

    No

    How would you label your Dimensions experience:

    Beginner

    Moderate

    Experienced

    contact.php:

    Thanks

    1. Roger Mercier Avatar
      Roger Mercier

      For some reason, the php code won’t post

    2. Kenneth von Rauch Avatar

      Chances are, your server does not support PHP. if you run it locally, you need to install a local server.

      You can do it as follows:

      http://webdesy.com/how-to-setup-a-local-server-localhost-on-your-mac/

      or

      http://webdesy.com/how-to-setup-a-local-server-using-wamp/

  10. Elizabeth Doidge Avatar
    Elizabeth Doidge

    Thankyou! Spent day after day trying to find a simple form to do exactly this. Tore my hair out. They were all so impossible for me to understand (I’m OLD) and all I wanted to do was to build a form, add checkboxes and send it. Exactly like yours does.Thankyou

    1. Vitaliy Kolos Avatar

      Hey Elizabeth, I’m excited to hear that I was able to help you!