Contact Form
This is a tutorial on how to create a basic contact form using PHP and is a follow on tutorial for the HTML Contact Form tutorial.
The tutorial doesnt go through how to add validation or any other fancy features, but it will give you a really good idea how the form works, and give you a few basic idea on how PHP works.
Steps
- Create a new folder on your desktop called "Contact"
- Move your contact form HTML file into that folder
- Create a new text file and name it "contact.php"
- Move the contact.php file into the Contact folder
- Paste the code below into the contact.php
- Replace the email subject and webMaster email address with your own.
- Near the bottom of the code there is a phrase that says "html results page", replace that with the HTML for your sucess page, or just type a sucess phrase as an alternative.
PHP:
<?php
$emailSubject = 'email subject';
$webMaster = '[email protected]';
$nameField = $_POST['name'];
$emailField = $_POST['email'];
$messageField = $_POST['message'];
$body = <<<EOD
Name: $nameField <br>
Email: $emailField <br>
Message: $messageField
EOD;
$headers = "From: $emailField\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
$theResults = <<<EOD
html results page
EOD;
echo "$theResults";
?>