Design Oahu

Web design blog from the middle of the pacific.

Practical PHP: Email Form

This is the second tutorial in the Practical PHP series. In the last tutorial we learned how to use PHP to create a simple site template for easily managing small websites. In this tutorial we will be creating a contact form which will mail the submission to your client’s email address. First of all the form itself:

<html>
<head>
<title>Contact Us</title>
</head>
<body>
<form action=”contact.php” method=”post”>
<label for=”contact_name”>Name:</label><br />
<input type=”text” name=”contact_name” /><br /><br />
<label for=”phone_number”>Phone Number:</label><br />
<input type=”text” name=”phone_number” /><br /><br />
<input type=”submit” value=”Submit” />
</form>
</body>
</html>

This should not be anything new, this is just a basic html page. We will call this page contact.html. The things that are important about this form is the action attribute which sends the information when submitted to the specified page, in this case contact.php. The way the information is sent is specified by the method attribute. We are using “post” which passes the data in the headers, the other option is “get”, which passes the data in the URL, like so: http://example.com/contact.php?contact_name=John&phone_number=555-5555. This wouldn’t be too bad, although not very pretty, but there is a limit of 2,048 characters in Internet Explorer, so we will stick with “post”. Now we will process this data in the PHP page contact.php

<?php
$name = strip_tags($_POST['contact_name']);
$phone = strip_tags($_POST['phone_number']);
$to = “name1@example.com, name2@example.com”;
$subject = “Website Contact”;
$headers = “From: mailer@example.com\r\n”;
$headers .= “Reply-To: mailer@example.com\r\n”;
$headers .= “Cc: Another Name <anothername@example.com>\r\n”;
$headers .= “Bcc: boss@example.com”;
$message = “$name just filled out the contact form on your website. Please contact $name at $phone.”;
mail($to, $subject, $message, $headers);
header(’Location: thank-you.html’);
?>

The entry that entered into the contact_name and phone_number fields is now available by using $_POST as that is the method we are using to pass the data. The strip_tags function prevents someone from entering code into your contact form. The mail function takes the following parameters: to, subject, message, headers. The to parameter is who the mail is going to. The subject is obviously the subject line, the message you guessed it, the message body. The headers are optional, but are a good habit to use. You should use an actual email account as the from and reply-to email address. This will reduce the chances that you message gets picked up as spam. You can cc and bcc extra email addresses, separate them by commas. Also, each line in the header has an \r\n which is separating the lines. After the mail is actually sent, we are just redirecting to a thank you page with the header function. So, that is the simple way to get your contact form going.

If you liked what you read you may be interested in subscribing to our RSS feed or email alerts. You may also want to read up about what RSS feeds are all about.

4 Comments

  1. Comment by raphiem on February 16, 2007

    Hi

    i was searching the net on how to customise page titles and found your site to be the easiest to understand …

    however i have a slightly more difficult situation and was wondering if you could kindly help me

    i have main.php page which includes

    menu.html

    footer.html

    and for the main body i use a

    with this i then call all my pages using

    main.php?page=otherpages.html

    now here is my problem and forgive me as php webdev is all new to me

    when i call otherpages.html … i want their page titles to be reflected in the browser etc as currently i am stuck with one page title for the whole website

    any assistance is appreciated

    regards

  2. Comment by Zwtwstyy on December 13, 2008

    Thanks!,

  3. Comment by D Jonas on February 22, 2009

    Hi there, I found your blog via Google while searching for first aid for a heart attack and your post looks very interesting for me.

  4. Comment by design guru on March 3, 2009

    I really liked the way they came off

Leave a Comment