HTML Form Basics : 22nd July 2007



Simple Contact Form

Providing your web host supports PHP, you just need to follow our example below to email the contents of a form to yourself. Firstly, this is the HTML code to put on your page:

<form action="submit.php" method="get">
   Name: <input type="text" name="name" /><br />
   Email Address: <input type="text" name="email" /><br />
   Message: <textarea name="message"></textarea><br />
   <input type="submit" value="Send Form" />
</form>

This will result in a form which looks similar to the following:

Name:

Email Address:

Message:



So you’re able to collect the data. Now to explain how to email it. To do this, you need to make a file called submit.php and put it in the same folder as your HTML file. It’s quite complicated to go into exactly how this works, but if you copy and paste the following code it should work fine:

<?php
$subject = "Website Message";
$message = "Someone sent you a message from your website:
Name: $name
Email: $email
Message: $message";
$youremail = "me@mywebsite.com";
mail($youremail,$subject,$message,"From: $email");
echo 'Thankyou for filling in a form!';
?>

All you need to do is change me@mywebsite.com to your actual email address. You’ll be receiving form submissions from your website in no time!

Other Form Options

HTML doesn’t just use text fields for collecting information. You also have the option to use checkboxes, radio buttons and drop down lists. Feel free to take and use the examples below:

Radio Buttons

These allow you to enforce that the visitor only makes one choice, unlike checkboxes which allow you to select multiple answers/settings. They look something like:

Red Blue Yellow

The code you use to create the above is:

<input name="favouritecolor" type="radio" value="red" /> Red
<input name="favouritecolor" type="radio" value="blue" /> Blue
<input name="favouritecolor" type="radio" value="yellow" /> Yellow

Checkboxes

Several checkboxes can be ticked, allowing multiple selections. It is as simple as changing the word radio above to say checkbox:

Red Blue Yellow

The code you use to create the above is:

<input name="favouritecolor" type="checkbox" value="red" /> Red
<input name="favouritecolor" type="checkbox" value="blue" /> Blue
<input name="favouritecolor" type="checkbox" value="yellow" /> Yellow

Drop Down Boxes

Drop down boxes work in a similar way to radio buttons, allowing the user to select one option. However, they save a great deal of space if you are dealing with a long list – selecting your country for example:

The code you use to create the above is:

<select name="favouritecolor">
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
<option value="pink">Pink</option>
</select>



Comments and Discussion


On 5th November 2007 , stuart anderson said:

I tried using this form but when i tested and checked my email i got a message from "Nobody" and there was no data from the text fields. whats happening?

On 24th January 2008 , Roadmap to Riches said:

Great tutorial! Great info and easy to understand. Thanks.



Add your thoughts



Subscribe


Design Shack CSS Gallery RSS Feed Design Shack News RSS Feed Design Shack Email Newsletter Design Shack Twitter


Tutorial Archives






Sponsored By


PSD to HTML, PSD to XHTML Service by PSD2HTML.com. You Design - We XHTML / CSS.

GoodBarry - The best way for web designers to build online businesses, not websites for their clients. eCommerce, Email Marketing, Content Management, Integrated CRM, Reporting and Analytics and web hosting in one integrated solution.

ACS: Social Media Marketing & Search Engine Optimization Agency

Gooey Templates

NuBlue UK Web Hosting

Logo design, pre-made logo design and stationery

Search Web Jobs

Standard Advert

I'd like to be
inspired by:



Colour


Red Designs Blue Designs Green Designs Yellow Designs Brown Designs Orange Designs Brown Designs Grey Designs Multi Coloured Designs


Category




Layout


One Column CSS Layout Two Column CSS Layout Three Column CSS Layout Grid CSS Layout Other CSS Layout


I've got a
question...



What is Design Shack?

We showcase stunning CSS and Web 2.0 design, alongside resources and inspiration for you to succeed in the same way. We only offer the cream of great design, perfect for getting that spark of creativity going again.

Can you feature my site?

Of course - if it's up to the mark! Just fill in our simple form.

Can I help out?

We are looking for new people to help update and maintain the website alongside us. If you're interested, feel free to contact us.