Simple Contact Form with PHP
In this tutorial, we’re going to create a Simple Contact Form with PHP which is a basic website form using only HTML (for the form) and PHP for the form processing.
Along-side the HTML form you will find a basic PHP script which will capture the form submissions and send the form contents to your email address.
Basic form in HTML
Here is the HTML form sample. You can edit the style of this to match your websites design.
Create an HTML file: contact.html
(you can change the filename to anything you like)
<form method="post" action="process-form.php"> <input type="text" name="name" placeholder="Your Name"><br> <input type="email" name="email" placeholder="Your Email"><br> <textarea rows="4" cols="20" name="message" placeholder="Your Message"></textarea><br> <input type="submit" name="send" value="Send"> </form>
The PHP Code which captures and Emails your website form
The PHP code below is very basic – it will capture the form fields specified in the HTML form above (name, email and message). The fields are then sent off to your email address in plain text.
Note: You need to edit 1 part of the script below. You need to tell it your email address (this will not be available for anyone to see, it is only used by the server to send your email).
Create a PHP file: process-form.php
(you must use this filename exactly)
if (isset($_POST['send'])) { $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $to = 'youremail@example.com'; $subject = 'Contact Request From Website'; $headers = "From: ".$name." <".$email."> \r\n"; $send_email = mail($to,$subject,$message,$headers); echo $send_email ? "Message sent!" : "Error sending message!"; }
Please note:
$to = 'youremail@example.com';
should be your admin or personal email address where you will receives contact requests.
Save the files above. Once you edit the form to fit with your design, you are ready to put it live. If you have any questions, please feel free to ask through comments section below.
For styling this contact form please visit this tutorial here Style a Contact Form with CSS