A Basic Email form

Lot’s of people seem to have form problems, so as well as cover a basic form, we’re going to have this email you as well.

Heres the form you need:
Form.html

<form action="send.php" method="POST">
Name: <input type="text" name="name"/>
Email: <input type="text" name="email"/>
Message: <textarea cols="50" rows="20" name="message"></textarea>
<input type="submit" value="Send"/>
</form>

Ok lets go over this first.

This line tells the browser we are starting a form. action is the file to call when the submit button is pressed, and method is how the browser will send information. This can be POST or GET (POST is recomended) This is a simple text input box. The most common type you will use is "text", "password" or "submit". The name is so we can call the information on the next page. This is the same for the email field.


This generates a text area box that is slightly bigger than the input fields. Cols is the width, and rows is the height.

Notice that the Input tags end in /> and text area has a closing tag. If something doesn’t have a closing tag, it’s opening tag should have a / before the closing > for it to be completely correct on a syntax checker, though the browser won’t through up any errors.

This generates a button to process the form.

Now we handle all the form stuff

send.php
[php]

<?php $Name = $_POST['name']; $Email = $_POST['email']; $Message = $_POST['message']; $Me = "[email protected]"; $Subject = "Message from your site"; $Headers = "From: {$Name} <{$Email}>"; mail($Me, $Subject, $Message, $Headers); ?>

Mail has been sent!
[/php]
Ok this is alot simpler to understand than it looks. the <?php[/b] tag simply tells the browser that anything now is php coding. This is ended with the [b]?> tag.

$Name = $_POST[‘name’];
$Email = $_POST[‘email’];
$Message = $_POST[‘message’];

Remember the name=“something” in the form? This is where it is called. In the form method we used POST, so to get the form info we take $_POST[‘something’] as above. If we used GET it would be $_GET[‘something’]

Remember ALL php lines must end in a semi-colon (:wink:

$Me = "[email protected]";

Just replace [email protected] with your email address.

mail($Me, $Subject, $Message, $Headers);
This just sends the email.
Congratulations you now have a fully working email form. If it doesn’t work, check you put everything correctly, remember variable names are case-sensitive and all lines end in semi-colons. If it still doesn’t send, check your email spam box, and then make sure your host has mailing enabled.

Sponsor our Newsletter | Privacy Policy | Terms of Service