Setting up a PHP Contact Form

Thursday 14 August 2008

article

Contact forms are a great way for visitors of your site to contact you.

They can be customised to fit in with the theme of your site, providing continuity and a richer user experience. Another advantage is that your email address is never visible as all the processing is done behind the scenes. This article will explain how to create a simple PHP contact form.

The contact form will span two pages. The first page will contain the HTML based form, while the second page will contain the PHP code responsible for organising the data from the first, into a standard email.

The HTML Page

First we will create the basic HTML page that contains the elements of the contact form. Just call it contact.html for now and insert the following code.

<html>

<head>
<title>My Contact Form</title>
</head>

<body>
<form method=”post” action=”mailer.php“>
<input type=”text” name=”name”
size=”20″><br>
<input type=”text” name=”email”
size=”20″><br>
<textarea rows=”9″
name=”message” cols=”30″></textarea><br>
<input type=”submit” value=”Submit”
name=”submit”>
</form>
</body>

</html>

The above code should create a very simple HTML page consisting of a few input fields and a button. When the submit button is clicked, all the data will be ‘posted’ over to file called mailer.php as specified in the form’s properties. Below is the code for mailer.php.

<?php
if (isset($_POST[‘submit’]))
{
$to = “your@email.com“;
$subject = “PHP Contact Form”;
$name_field = $_POST[‘name’];
$email_field = $_POST[’email’];
$message = $_POST[‘message’];

$body = “From: $name_fieldn
E-Mail: $email_fieldn Message:n

$message”;

mail($to, %subject, $body);
echo “Email has been sent!”;
}
else
{
echo “No data available!”;
}
?>

Copy the above code into mailer.php and replace your@email.com with your own email address. Upload both contact.html and mailer.php to the same directory on your website. Open contact.html in your browser and fill out all the fields then click submit. If everything working out, you should receive an email to the address you specified in mailer.php.

The Code

<form method=”post” action=”mailer.php“>

This line tells the form where to send the data, which in this case is mailer.php. The rest of the code is standard HTML. The important parts to note are the names given to the fields.

<input type=”text” name=”name” size=”20″><br>
<input type=”text” name=”email” size=”20″><br>
<textarea rows=”9″ name=”message” cols=”30″></textarea><br>
<input type=”submit” value=”Submit” name=”submit”>

They have no effect on how the form looks, they simply help to categorise the data, which is very useful when dealing with larger forms. It is best practice to call your fields something relevant to the information that will be entered into them.

Now for the PHP code.

if (isset($_POST[‘submit’]))

This is a useful piece of code to check if the user actually pressed the submit button on contact.html. If not, the ‘submit’ variable would not be set and the page would display the error message instead.

$to = “your@email.com;
$subject = “PHP Contact Form”;

These next two lines create two variables that store your email address and the subject.

$name_field = $_POST[‘name’];
$email_field = $_POST[’email’];
$message = $_POST[‘message’];

These three line create variables that extract and store the data sent over from contact.html. The data is collected in an array called $_POST.

The data is accessed by specifying the form field name from contact.html. It should now become apparent why careful naming on HTML form’s can make things a lot easier.

$body = “From: $name_fieldn E-Mail: $email_fieldn Message:n
$message”;

The $body variable simply combines all the previous variables into a single one, which can be used with PHP mailing function.

mail($to, %subject, $body);

This is where the actual mail gets sent. The mail function of PHP takes three arguments. The first is simply the email address that the email should be sent to. The second specifies the subject of the email, and the third is the body of the email. This is why it was necessary to combine all the previous variables into a single one called $body.

echo “Email has been sent!”;

This will simply print a message letting you know the email has been sent.

If you have any questions regarding managed dedicated server hosting, please contact our support team on 0808 1 333 247.

cwcs

CWCS are here to look after your online infrastructure, so you can look after your business!

Return to blog page

Put Your Servers In Safe Hands With Our Supreme Cloud Hosting

View our services to see which of our hosting options best suits your needs.

ISO 27001 Certified

Compliant to ISO 9001:2015 & ISO 27001:2013, CWCS is independently audited for your assurance that you will receive the highest level of quality, and will ensure your information secure, intact, and only available to those authorised to access it.

G-Cloud Supplier

The UK Government G-Cloud is an initiative targeted at easing procurement by public-sector bodies in departments of the UK Government of commodity information technology services using cloud computing.

Cyber Essentials

Cyber Essentials certifiication demonstrates our commitment to cyber security. Government and industry have worked together to produce a set of standards which helps organisations safeguard against the most common cyber threats.

Call us on 0800 1 777 000 or email us at sales@cwcs.co.uk