• Welcome to ForumKorner!
    Join today and become a part of the community.

Contact Form PHP Script

OliverE

Power member.
Reputation
0
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
  1. Create a new folder on your desktop called "Contact"
  2. Move your contact form HTML file into that folder
  3. Create a new text file and name it "contact.php"
  4. Move the contact.php file into the Contact folder
  5. 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";
?>
 
Top