Ajax Contact Form
Below is a simple way to use Ajax for a website contact form. I’ve created the full thing using one script (which can be added to any page using the php include function).
I’ll come back to this page in the near future to explain more about how the script hangs together - but if you have experience with PHP and JavaScript it will look pretty straightforward.
If you decide to use this - please note I have not fully tested this for bugs or security flaws, so use at your own risk.
Hopefully you can extend this to fit your own needs.
So here it is - just save it as ajax_contact.php and use: include ‘ajax_contact.php’; in you php file to have it work. (remember to update the email address!)
if(!isset($rnd) || !isset($name) || !isset($email) || !isset($subject) || !isset($body)) {
showform();
} else {
processform();
}
function processform(){
global $name, $email, $subject, $body;
$email_to1 = "foo@hotmail.com"; // enter your email here
$email_from1 = $mail;
$email_to2 = $mail;
$email_from2 = $email_to1;
$email_subject = "Contact Form: ".stripslashes($subject);
$email_message = "Please find below a message submitted by '".stripslashes($name);
$email_message .="' on ".date("d/m/Y")." at ".date("H:i")."\n\n";
$email_message .="--------- START OF SUBMITTED MESSAGE ---------\n\n";
$email_message .= stripslashes($body);
$email_message .="\n\n--------- END OF SUBMITTED MESSAGE ---------\n\n";
$confirmation_subject = "Thank you for your message";
$confirmation = "This is to confirm we have received your message....";
// SEND EMAIL TO email_to2 - confirmation
$headers = 'From: '.$email_from2."\r\n" .
'Reply-To: '.$email_from2."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to2, $email_subject, $confirmation, $headers);
// SEND EMAIL TO email_to1 - message to you!!
$headers = 'From: '.$email_from1."\r\n" .
'Reply-To: '.$email_from1."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to1, $email_subject, $email_message, $headers);
echo "Thank You.";
die();
} // end processform()
function showform() {
?>
} // end showform()
?>
