PHP Contact form

It is not a good idea to place a contact email in your web. Someone may use a robot to find your email and send spam. Instead, it is recommended to use a contact form. Alternatively, you may use javascript to hide your complete email (info).

You will find bellow a copy and paste contact form. You just need to copy the code to a file named contact_form.php, change emails in red, and place it in your site. Them add a link to the contact form in your regular pages.


contact_form.php
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
<html><head><title>Contact Form</title></head>
<body bgcolor=FFFFFF>

<center><p><center>
<H1>Suggestions/Questions about mysite.com</h1>
<table><tr><td>

<?
$text=$_POST["text"];
$email=strtolower($_POST["email"]);
if (strpos($email,"@")==0 or strpos($email,".")==0){$email="";}
if ($email!="" and $text!=""){
   $message="$email\n\n$text";
   mail("webmaster@mysite.com","Contact form", $message,"From: <webmaster@mysite.com>\nContent-Type: text/plain");
   print "<center>Thanks for your Suggestion/Question<p><a href=/>Home Page</a></center>";
}else{
?>

<form action=<? print $_SERVER["PHP_SELF"]; ?> method=post>
<p>Your contact email:<br>
<INPUT NAME="email" SIZE="35" MAXLENGTH="65">
<BR>Your text:<br><TEXTAREA name=text cols=40 rows=5><? print $text; ?></TEXTAREA>
<br><INPUT TYPE="SUBMIT" NAME=submit VALUE=Submit>
</form>

<? } ?>

</td></tr></table>
</center></body></html>

Lines 1-6: Just page title and headings

Lines 8-17: php code

Line 9: Text introduced by user is saved to variable $text.
Line 10: email is saved to variable $email. By using strtolower, we will get the email in lower case.
Line 11: It is checked whether email is correct (simple checking). The command strpos is used to check position of "@" and "." within $email. If position is "0", $email is not correct and email information is removed (is set to "").
Line 12: If value of $email or $text is not null, lines 13 to 15 are executed. If not, the form in lines 20-25 is displayed.
Line 13: A new variable named is created which contects the email value and text of user. The code "\n\n" means we are separating email and text with two line breaks.
Line 14: The command mail is used to send email.
Line 15: A "Thanks you" message is shown.

Lines 20-25: If message is not send (for example when getting to the page from a link), the form is displayed.

Line 23: In case a user has introduced a message in the textbox, but has forgotten to include email, as $email="", mail is not send to webmaster, and the form is displayed. In this case, the code "<? print $text; ?>" is used to include the message in the textbox.

Lines 29-30: Finishing the page.

PHP Contact form (version 2)

The contact form above may be use by spanners to send you ads and other span message. The new version bellow includes a bit of code (in magenta) to avoid this fraudulent usage.


contact_form.php
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
<html><head><title>Contact Form</title></head>
<body bgcolor=FFFFFF>

<center><p><center>
<H1>Suggestions/Questions about mysite.com</h1>
<table><tr><td>

<?
$text=$_POST["text"];
$email=strtolower($_POST["email"]);
if (strpos($email,"@")==0 or strpos($email,".")==0){$email="";}
if ($email!="" and $text!=""){
   $jcontrol=$_POST["javascriptcontrol"];
   if ($jcontrol=="") {die("Error: This service does not believes you have not visited the website with a regular browser.");}
   $message="$email\n\n$text";
   mail("webmaster@mysite.com","Contact form", $message,"From: <webmaster@mysite.com>\nContent-Type: text/plain");
   print "<center>Thanks for your Suggestion/Question<p><a href=/>Home Page</a></center>";
}else{
?>

<form name=AA action=<? print $_SERVER["PHP_SELF"]; ?> method=post>
<p>Your contact email:<br>
<INPUT NAME="email" TYPE=text SIZE="35" MAXLENGTH="65" onchange="document.AA.javascriptcontrol.value='dd';">
<INPUT NAME="javascriptcontrol" TYPE=hidden value="">
<BR>Your text:<br><TEXTAREA name=text cols=40 rows=5><? print $text; ?></TEXTAREA>
<br><INPUT TYPE="SUBMIT" NAME=submit VALUE=Submit>
</form>

<? } ?>

</td></tr></table>
</center></body></html>

How it works:
When email is included in the form, a javascript code (in line 23) will add the value "dd" to the hidden variable "javascriptcontrol" (in line 24). In case javascript is not available in the browser, this change will not happen (for example when a robot visits our page, fills the form and submits it without using javascript). In lines 13 and 14 it is checked whether the value for the variable "javascriptcontrol" exists. If there is no value for this variable, an error is reported (and no email is send).

Display contact email by using javascript

The code bellow may be used to display a link for contact, but avoiding robots from obtaining the address.
This is not a PHP program.

javascript code
Output example
<script language="JavaScript" type="text/javascript">
document.write("<A HREF=\'mailto:myname");
document.write("@");
document.write("mydomain.com\'>");
document.write("Contact email");
document.write("</A>");
</script>
Contact email

How it works:

The link which includes the contact address is printed out only when javascript is available. Consequently, a robot will not find the email. Just modify the email in red and copy the code to the exact location in the page you want the contact link to be displayed.



phptutorial.info