Be the first to write a review
Free - Email form with Spry AJAX
In this tutorial you will learn how to build AJAX email form using the Spry Data Utilities Toolkit. As a result you will be able to turn your traditional email form into an AJAX-based one or build one from scratch.
Although for the purpose of this tutorial we will use PHP, the same approach can be adapted for any other server model.
Creating Email Form
In Dreamweaver create a new form and add all the fields you need to include in the email message (email address, message, phone number, etc) or use your existing email form.
Make sure the action parameter of the form is empty and the method is set to "POST".
Email Code
Add the following code in the Code View at the top of your web page:
<?php
if(isset($_POST["Submit"])){
$to="me@mydomain.com";
$from=$_POST["Email"];
if($from == "){
echo "Please specify your email
address";
die();
}
//At first the message is blank
$message=";
//Loop through all the field and put
together the message
foreach($_POST as $fieldName =>
$value){
if($fieldName!="Submit"){
$fieldValue=$value;
$message=
$message."<b>$fieldName</b>: $fieldValue<br>";
}
}
$html_header.= "\r\nContent-type:
text/html; charset=iso-8859-1;";
$headers="From:".$from.$html_header;
$subject="Message sent from web
site";
if(mail($to, stripslashes($subject),
stripslashes($message),$headers)){
echo "The message has been
sent";
die();
}else{
echo "Error sending message";
die();
}
}
?>
Submitting the form will trigger the above
code.
Let's quickly review it so you can understand how it works.
First the script checks whether the form has been submitted:
if(isset($_POST["Submit"])){
For that to work your Submit button should
have the name attribute "Submit". If it doesn't change the script to match your
button name.
Variables $to and $from store email addresses of the recipient and the sender:
$to="me@mydomain.com";
$from=$_POST["Email"];
The latter is based on the assumption that your email form field's name
attribute is "Email". If not either adjust the script or the name of your email
field.
If the email field is empty output the error and stop the script:
if($from == "){
echo "Please specify your email
address";
die();
}
Otherwise loop through all the fields and put together the message in the "Field name: Field value" format:
foreach($_POST as $fieldName =>
$value){
if($fieldName!="Submit"){
$fieldValue=$value;
$message=
$message."<b>$fieldName</b>: $fieldValue<br>";
}
}
Set the format of the message to HTML and add the message header:
$html_header.=
"\r\nContent-type: text/html; charset=iso-8859-1;";
$headers="From:".$from.$html_header;
Add a subject line:
$subject="Message sent from web site";
Attempt to send the message. Display "success" or "error" (depending on the results) at the end and terminate any output:
if(mail($to, stripslashes($subject),
stripslashes($message),$headers)){
echo "The message has been
sent";
die();
}else{
echo "Error sending message";
die();
}
Alex July
Alex July is a Vancouver-based (Canada, British Columbia) Web Developer/ Graphic Artist who has an extensive experience in both creative realms.
He is also a host of Linecraft.com where he is showcasing his skills and sharing experience with the developers community. For the past 3 years Alex has been focusing on the development of Rich Internet Applications using Macromedia Flash technology.
When away from the computer Alex is practicing Martial Arts, playing guitar and enjoying time with his wonderful family.