Build a quick and simple Contact Form
August 3rd, 2012 - 3 Comments »
Need a quick and easy contact form for your website? Then this is the code for you, It is a very simple script.
Step 1: The Markup
Create a new file, call it contact.html or similar. Now let’s actually create the form fields. Copy and paste the following code into the newly created .html file.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<form method="post" action="contactengine.php"> <label for="Name">Name:</label> <input type="text" name="Name" id="Name" size="45" /> <br /> <label for="Tel">Tel:</label> <input type="text" name="Tel" id="Tel" size="45" /> <br /> <label for="Email">Email:</label> <input type="text" name="Email" id="Email" size="45" /> <br /><br /> <label for="Message">Details of Quote / Query</label><br /> <textarea name="Message" rows="5" cols="45" id="Message"></textarea> <input type="submit" name="submit" value="Submit" class="submit-button" /> </form> |
As you may notice the forms “action” is “contactengine.php” this file, which we will create in the next step is the functioning of the whole contact form. Now save and close this file.
Step 2: The workings of the Contact form
Now create a file called contactengine.php This is the workings of the contact form, all the magic happens in here. Copy and paste the following code into this file.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php $EmailFrom = "noreply@yoursite.com"; // From email address $EmailTo = "youremail@yoursite.com"; // Email address you want submitted forms to go to $Subject = "YourSite.com - Contact Form Submission"; // subject line for emails $Name = Trim(stripslashes($_POST['Name'])); $Tel = Trim(stripslashes($_POST['Tel'])); $Email = Trim(stripslashes($_POST['Email'])); $Message = Trim(stripslashes($_POST['Message'])); // validation $validationOK=true; if (!$validationOK) { print "There has been an error, please make sure you entered a correct email address."; // You can edit this to your own error message exit; } // prepare email body text $Body = ""; $Body .= "Name: "; $Body .= $Name; $Body .= "\n"; $Body .= "Tel: "; $Body .= $Tel; $Body .= "\n"; $Body .= "Email: "; $Body .= $Email; $Body .= "\n"; $Body .= "Message: "; $Body .= $Message; $Body .= "\n"; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); // redirect to success page if ($success){ print "Thank you, your email has been sent! We will be in touch shortly!"; // You can edit this to your own success message } else{ print "There has been an error, please make sure you have entered your details correctly."; // You can edit this to your own error message } ?> |
This code basically grabs all of the detials input into the contact form and sets them out into an email which you will receive to the email specified.
You can edit the parts I have commented to your own details / messages. Now save this file in the same directory as the contact.html file and upload to your server…
Voila, a simple and quick email form for your website. Very easy to integrate and style to suit your own websites. If you have any trouble please just leave a comment and we will be happy to help you out.
Related posts:









Please help me i used the codes to fit in a form on my site but it returns
”There has been an error, please make sure you have entered your details correctly. ” where could the problem be pls
i only changed the Email to – my email.
Thanks
hi
thnkx for ur script
i have been done which as u mentioned here. And in the test run message ” u r message has been sent ”
but nothing else to come in my email
can u plz help me out?
thnkx
dear raju you can try on live server ratherthan localhost..