First we want to create the basic form using standard HTML (Hyper Text Markup Langauge), then we will style it using CSS (Cascading Style Sheets) and after that we will make it all work with PHP (Personal Hypertext Preprocessor).
All of the following code must be placed within the <body>...</body> section of the webpage
<?php $action=$_REQUEST['action']; if (==""){ ?>
This PHP command (along with a hidden form field further down) tells the form to do nothing until the Submit button is pressed. Without this command, the form will simply try to send as soon as the page is loaded when all the form fields are empty.
To create the forms fields, we will decide what fields we want and then we will make a div (division - think of it like a box) to put it all in. Each form field will be within its own div so that we can easily and neatly add a name or 'label' to it (we will call these 'formcell' to identify them in the CSS styling later). All of the seperate field divs will be encompased by a main div which will keep everything together.
For a basic form, we want a name field, email address field, email address confirmation, subject and message.
Email address confirmation is handy as it detects typos or other errors. If the confirmation field does not match the email field, it will cause an error and ask the user to go back and check. Without this, its easy for someone to mistype their email address and you will not be able to reply to their message.
<form action="contact.php" method="POST">
Start the form code. "contact.php" is the file name of the contact form, it can be whatever you like, but the filename of the form and the filename here must be exactly the same or the form will not work properly.
<input type="hidden" name="action" value="submit"/>
This statement goes along with the PHP command above. If the form is not submitted then the "action" string remains empty and the form will wait until the submit button is pressed before doing anything. The user does not need to see this displayed within the form so we use the input type="hidden"
<div class="form">
This says we want to open a div. The 'class=form' command tells it that we want to use the styling called 'form' thats associated with a div (in this case div.form which we will create in the style commands later)
<div class="formcell"><span class="formlabel">Name:</span><input type="text" name="Name" class="box"></div>
Open a 'formcell' styled div, Place the label of the form field inside a 'formlabel' styled span. We could use another div for this, but for just one or two words a div is overkill, so we use a span instead. A span surrounds several words and allows us to easily style them altogether.
Next we tell the form we want the user to enter some information. We use the 'input' command for this, and its type is text (because we want the user to enter text of course!). The 'name=' is very important, as later in the code this is what the PHP will use to identify the form field. You can name it whatever you like, but for simplicity -as this is the name field- we will call it name. The 'class=' command does exatly the same as the one in the above div only now it relates the style 'box' with the input command. Then close that div. This is now repeated -with different labels and names- for the email, confirmation and subject fields.
<div class="formcell"><span class="formlabel">E-mail:</span><input type="text" name="Email" class="box"></div>
<div class="formcell"><span class="formlabel">Confirm E-mail:</span><input type="text" name="Confirm" class="box"></div>
<div class="formcell"><span class="formlabel">Subject:</span><input type="text" name="Subject" class="box"></div>
<div class="formcell"><span class="formlabel">Message:</span><textarea name="Message" rows="4" class="box"></textarea></div>
This line is very similar to the 4 above, but as we expect more than two or three words of text (since this is where the main message will be typed) we now use the 'textarea' command instead.
'rows=' simply tells the form how many lines (or rows) of text the textarea will hold. Basically this is the height of the textbox, more than 4 lines of text can be entered and it will simply scroll up. You can use as many rows as you like in your form, I use 4 as I think it looks proportional to the rest of the form.
To check its a human and not a spam-bot we can ask a question and have a drop down box to select the answer. It can be a very simple question, just don't phrase it as a typical question. For example, we use "Mary Had" instead of "What did Mary have?"
So, here is the code for the question:
<div class="formcell"><span class="formlabel">Mary had:</span><select name="Question" class="box">
As we do not want the user to type in anything, we use the 'select' statement to tell it we want a drop-down select field instead. Notice that we do not close the select statement yet, we first need to tell it what to put in the drop down list. To do this we use the 'option' statement.
<option value="an empty box">Select...</option>
The 'value=' of the option statement is what the HTML and PHP code will see, and the text outside of the option chevron is what the user will see. Normally both of these will be the same, but the first option is shown when the form is displayed, so we would let the user know they need to select an option and use a different word or phrase to let the code know nothing has been selected. Our question is 'mary had' and so if nothing is selected we tell the code that mary had an empty box. The next 5 options are selectable answers to the question.
<option value="a little goose">a little goose</option>
<option value="a little duck">a little duck</option>
<option value="a little lamb">a little lamb*</option>
<option value="a little cow">a little cow</option>
<option value="a little goat">a little goat</option>
</select></div>
Now we have entered all our options, we can end the select statment code and close the div.
You can use as many options as you like, to make it harder for spam-bot to guess the correct answer, just don't add TOO many and confuse the actual humans! You can see that I've added a star to the correct answer, just incase someone never heard of Mary and her Lamb before ;-) The star doesn't seem to cause any problems with the spam-bots, so all good. You could also add another question, but for now we are just keeping it simple.
<div class="formcell"><input type="submit" value="Submit"/></div>
This code puts a submit button below the form, allowing the form to be sent.
</div></form>
Close the main form div and end the HTML form code.