Oct
6

How to create a simple PHP Contact Form

Author Toma    Category Coding     Tags


Having a contact form on your web site is very useful since the visitors can send you direct e-mails from your web sites. This contact you can place it anywhere you want : i usually have it in the contact page or guest book or request a quote page. You can also place the form anywhere you want a feedback . The only condition is that the server you are hosting the page has to support php code.

Here are the simple steps that need to be done :
– first we have the code to manage the information send by the form;
– then we have a java script function that checks if the fields are not empty;
– and finally the form it self.

Remember that the page should have a .php extension.

Here is the code :
This is the piece of code that is placed right at beginning of the file and that will handle the information send by the form. You notice that I have an e-mail address bellow that you can replace it with whatever address you want. Additional to the message this code will add a text to the subject : Message from website so that you know when an e-mail is sent directly from your web site.
<?php
$subject = addslashes($_REQUEST["subject"]);
$content = addslashes($_REQUEST["content"]);
$email = addslashes($_REQUEST["email"]);
$send = addslashes($_REQUEST["send"]);
$trimis = addslashes($_REQUEST["trimis"]);
$verify = addslashes($_REQUEST["verify"]);


$emailCheck = “/^[-+\\.0-9=a-z_]+@([-0-9a-z]+\\.)+([0-9a-z]){2,4}$/i”;


if($send==1 and $subject!=”" and $content!=”" and $email!=”" and $verify==”10″)
{
$subject2 = “Message from website:”.$subject;
mail(“bonciutoma@yahoo.com”, $subject2, $content, “From:”.$email);
$send = 0;
$subject = “”;
$content = “”;


$email = “”;
header(“Location: guest.php?trimis=1″);
exit;
}


?>

The java script function that check for the fields is this :

<script type=”text/JavaScript”>
function VerificaForm() {
var form = document.forms[0];
var bRequired = true;
if ((form.subject.value.length < 1) || (form.email.value.length < 1) || (form.content.value.length < 1) || (form.verify.value.length < 1))
{
alert(“Completati va rog toate campurile”);
bRequired = false;
}


if (!bRequired) return false;


form.submit();
}
</script>

And finally the form that you can place it where you want on the page(don’t forget to change the page name where the form is placed in the code beloow) :

<form action=”contact.php” method=”get” enctype=”multipart/form-data” name=”email”>
<label> <span class=”textFormat”>Subject</span><br>
<input name=”subject” type=”text” class=”ContactForm” id=”subject” size=”40″ maxlength=”40″>
</label>
<br>
<label><span class=”textFormat”>Your email adress </span><br>
<input name=”email” type=”text” class=”ContactForm” id=”email” size=”40″ maxlength=”40″>
</label>
<br>
<label><span class=”textFormat”>How much is 5+5 ?</span><br>
<input name=”verify” type=”text” class=”ContactForm” id=”email” size=”40″ maxlength=”40″>
</label>
<br>
<br>
<label><span class=”textFormat”>Message</span><br>
<textarea name=”content” cols=”40″ rows=”15″ class=”ContactForm” id=”content”></textarea>
</label>
<input name=”send” type=”hidden” id=”send” value=”1″>
<br>
<label>
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input name=”Update” type=”submit” class=”ContactForm” id=”Update” onClick=”VerificaForm(); return false;” value=”Send email”>
</label>
<br>
</form>

There are also two classes that you’ll have to create in CSS if you want to format the labels and the fields : textFormat and ContactForm.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
If you find the content from this blog useful to your business please consider subscribing so that you'll receive my articles in your e-mail!

Post comment