Heartless Adrenaline

PHP Forms

HomeTutorials → PHP Forms

Many people use great online sources such as Email Me Form and My Contact Form to create contact forms, but an easier and more effective way is to use a simple PHP code to create your form.

For this tutorial, you will need:

Creating the Form

First, you need the fundamental form tag to surround your inputs. Copy & paste this code to the start of where your form is displayed:

<form method="POST" action="yourdocument.php">

The method tells the form what to do with the information where the action is where it will be sent. Replace "your document" with the address of the PHP page you created (we'll create the contents of that page later).

You're probably familiar with the several types of form inputs available, but here's a list of them; if you want to move to making the form send, click here.

Text Input

<input type="text" name="NAME" SIZE="30">

The 'text' makes it able to be typed in; replace "NAME" with the individual name you'd like the input to have (no two inputs in the same form should have the same name); replace "30" with the number of columns, essentially, that you'd like the input to have.

Example:

Textarea>

<textarea name="NAME" rows="3" cols="30"> </textarea>

The textarea is where users can type freely without a limit, extending their text space downwards when needed. Replace "NAME" with the individual name you'd like the input to have (no two inputs in the same form should have the same name); replace "3" with the number of rows you'd like; replace "30" with the number of columns you'd like.

Example:

Radio Buttons

<input type="radio" name="NAME" value="ONE">Value 1
<input type="radio" name="NAME" value="TWO">Value 2

Radio buttons are where users can select only one option from a list of various options. Replace "NAME" with the individual name you'd like the input to have (no two inputs in the same form should have the same name, but the radio inputs must have the same name for them to be in the same set); replace "ONE" and "TWO" with the individual values you'd like each button to have in the e-mail; replace "value 1" and "value 2" with those values so that your viewer can see them. * Remember: numbers (1, 2, 3, etc.) do not make good values, id's, or classes.

Example:
Value 1
Value 2

Checkboxes

<input type="checkbox" name="NAME" value="ONE">Value 1
<input type="checkbox" name="NAME" value="TWO">Value 2

Checkboxes allow users to select more than one option from a given set. Replace "NAME" with the individual name you'd like the input to have (no two inputs in the same form should have the same name, but the checkbox inputs must have the same name for them to be in the same set); replace "ONE" and "TWO" with the individual values you'd like each button to have in the e-mail; replace "value 1" and "value 2" with those values so that your viewer can see them. * Remember: numbers (1, 2, 3, etc.) do not make good values, id's, or classes.

Example:
Value 1
Value 2

Dropdown Menus

<select name="NAME"> <option value="ONE">Value 1</option> <option value="TWO">Value 2</option> </select>

Dropdown menus allow users to select one of various options, with the display only taking up one line, which can be way better for longer lists. Replace "NAME" with the individual name you'd like the input to have (no two inputs in the same form should have the same name, but the dropdown menu must have the same name for it to be a single set); replace "ONE" and "TWO" with the individual values you'd like each button to have in the e-mail; replace "value 1" and "value 2" with those values so that your viewer can see them. * Remember: numbers (1, 2, 3, etc.) do not make good values, id's, or classes.

Example:

The Submit Button

Now comes the easy part: putting the submit and reset buttons at the end of your form. Here are the two codes:

<input type="submit" value="I'm Done!"> <input type="reset" value="Oops!">

The 'submit' and 'reset' values are essential to making them work properly; the values "I'm Done!" and "Oops!" are changeable, depending on what you'd like the buttons to display. You can replace them with any number of expressions, even "submit" and "reset."

Example:

Making the Form Send

Here comes the fun, though surprisingly easy, party of creating a form. Simply copy & paste the code below into your PHP page for the sending code (the second page mentioned in the introduction) and edit it accordingly. Do not put any other coding into that page.

<?php $NAME = $_REQUEST['Title you'd like here'] ; $EMAIL = $_REQUEST['email'] ; $MESSAGE = $_REQUEST['message'] ; mail( "YOUR EMAIL HERE", "SUBJECT OF MESSAGE HERE", $MESSAGE, "From: $EMAIL" ); header( "Location: THANK-YOUPAGE.PHP" ); ?>

As you can see in the second line, $NAME is where you'd replace "NAME" with the field names you gave earlier. You can change "Title you'd like here" to any title you want to appear in the e-mail; for example, your input name might be "dog," but you might want your e-mail to say "breed user has of dog." Create as many lines of that code as you have input names!
"EMAIL" and "MESSAGE" are not automatically created for you; you must create them yourself. However, they are used here to display that the "$MESSAGE" inputted into the mail section (underlined) will be the message you recieve. The "$EMAIL" (underlined) will be the display e-mail of who the message is from.
Remember to change these fields according to your input names.

Now, just change "YOUR EMAIL HERE" to your e-mail address, "SUBJECT OF MESSAGE HERE" to the subject you'd like the e-mail to have, and "THANK-YOUPAGE.PHP" to the URL of your thank-you page.

Create your thank-you page, and you're all set! :)

I hope that this tutorial helped you! If you still have any lingering questions, please don't hesitate to contact me here.