Website & Apps

How to make a Working PHP Contact Form

PHP_contact_form

Everyone wants to have a Contact form on their website in order to know what your website visitors think about your website. Here we discuss how to make a working PHP contact form, from which you will be able to receive thoughts, feedback, comments of your website visitors to your email without making your email address visible to your website visitors.

First create a simple HTML form with the Text Fields you want to place on your website. I am creating here 3 fields; the name, email and comments. You can add as more as you want and make it working with a little amendmentĀ  in PHP Code.

See below the HTML coding for the 3 aforementioned form fields:

<form name=”contact_form” action=”contact.php” method=”post”>
<table width=”100%” border=”0″ cellspacing=”2″ cellpadding=”2″>
<tr>
<td width=”25%”>Your name:</td>
<td width=”75%”><input name=”name” type=”text” id=”name” size=”32″></td>
</tr>
<tr>
<td>Email address:</td>
<td><input name=”email” type=”text” id=”email” size=”32″></td>
</tr>
<tr>
<td>Comment:</td>
<td><textarea name=”comments” cols=”45″ rows=”6″ id=”comments”></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align=”left”><input type=”submit” name=”Submit” value=”Send”></td>
</tr>
</table>
</form>

We now have to create a PHP file named ‘contact.php’ to make this form in working condition. See the PHP code below:

<?php

/* Subject and Email variables */

$emailSubject = ‘Email from your Website!’;
$webMaster = ‘——enter email here where you want the filled information to be sent to. e.g: youemail@yourdomain.com—–‘;

/* Gathering Data Variables */

$nameField = $_POST[‘—-name of the NAME textfield of the form—-‘];
$emailField = $_POST[‘—-name of the EMAIL textfield of the form—-‘];
$commentsField = $_POST[‘—-name of the COMMENTS textarea of the form—-‘];

$body = <<<EOD
Name: $nameField n
Email: $emailField n
Comments: $commentsField n
EOD;

$headers = “From: $emailFieldrn”;
$headers .= “Content-type: Texthtmlrn”;
$success = mail($webMaster, $emailSubject, $body, $headers);

/* Results rendered as HTML */

$theResults = <<<EOD
——-Place Complete HTML of the page you want to be appeared after the form has been submitted, here.——
EOD;
echo “$theResults”;

?>

Place the above code in the Adobe Dreamweaver, or whatever HTML editor you are using and Save it with the name ‘contact.php’. You need to change the text in RED. Write the Email Address where you want to receive the email of the submitted form. See below:

$webMaster = ‘youemail@yourdomain.com’;

Write the name of the ‘name’ text field. As we have used the ‘name’.

$nameField = $_POST[‘name’];

Now:

$emailField = $_POST[’email’];
$commentsField = $_POST[‘comments’];

At this step, coding for email the form is completed. Now what you want to show your visitor after his clicked on the submit button. Surely it would be a page displaying ‘Thank you for contacting me.’ or something like this. Now make a duplicate page of the HTML page containing your form fields and remove the FORM from it. Write the ‘Thank you’ text there. Now copy the whole HTML coding and place it:

$theResults = <<<EOD

“<html
<head></head>
<body>
—Your thank you text—-
</body>
</html>”
EOD;

Finally upload the files to your server, contact.php and the HTML file containing the FORM fields and you have successfully created your own PHP CONTACT FORM for your website!!! šŸ™‚

How to Attribute

You have to attribute CreativeAlys for free design resources you download from this website.

Design by creativealys.com

Copy the above line and paste it in a visible place, close to where youā€™re using the design resource.