Thursday, February 19, 2009

Adding a php contact form

Create a form with the action calling itself, and the method is POST

The form should have 2 input text boxes named, name, email and a textarea named question

Have added some JavaScript information to validate the contents of the form, at this post 

Add the folowing to the start of the php file

$name = $_POST['name'];
$email = $_POST['email'];
$question = $_POST['question'];
$emailto = "email@yourwebsite.com";

Include the following to call a function

include "function_contact_form.php";
contact_form($name,$email,$question,$emailto);

Create a file called function_contact_form.php and add the text below.

function contact_form($name,$email,$question,$emailto)
{
$myemail = $emailto;

$subject = "Ask A Question";

$message = "From: $name ($email) \n
Question: $question \n";

$from = "From: $email\r\n";

$spamerrormessage = "A web site URL has been detected, the form submission has been cancelled";
if (preg_match("/http/i", "$name")) 
{
echo " $spamerrormessage"; 
exit();
}
if (preg_match("/http/i", "$email")) 
{
echo " $spamerrormessage";
exit();
}
if (preg_match("/http/i", "$message")) 
{
echo " $spamerrormessage"; 
exit();
}


if ($myemail !="")
mail ($myemail, $subject, $message, $from);
echo "Thank You $name for your inquiry.";
}

No comments:

Post a Comment