AJAX implementation of Formmail
If you need or want to use something other than the venerable CGI formmail.pl, you can use this AJAX based version.Installation is simple. You need a copy of the Prototype library. If you're doing AJAX you already have a copy or two laying around somewhere.Then download and modify the two files included.If you just want to look at the code, then here it is.First the user facing HTML code:
<html>
<head>
<title>AJAX/PHP implementation
of formmail.pl</title>
<script src="js/prototype-1.4.0.js"></script>
<script>
function ajax_formmail()
{
var email =
$F('email');
// modify
url to point to the location on your web server
var url =
'http://localhost:82/ajax_formmail.php';
var pars =
'email=' + email;
var myAjax
= new Ajax.Request(
url,
{
method:
'get',
parameters:
pars,
onComplete:
showResponse
});
}
function
showResponse(originalRequest)
{
// hide the
input div
$('ajax_formmail').style.visibility
= 'hidden';
//put
returned text in the result div
$('ajax_formmail_result').innerHTML
= originalRequest.responseText;
$('ajax_formmail_result').style.visibility
= 'visible';
}
function ajax_formmail_clear()
{
$('ajax_formmail_result').style.visibility
= 'hidden';
$('ajax_formmail').style.visibility
= 'visible';
}
</script>
<style type="text/css">
#ajax_formmail_container {
position: relative;
width: 350px;
}
#ajax_formmail {
position: absolute;
width: 300px;
left: 0px;
top: 0px;
z-index: 2;
}
#ajax_formmail_result {
position: absolute;
width: 300px;
left: 0px;
top: 0px;
z-index: 1;
}
</style>
</head>
<body>
<div id="ajax_formmail_container">
<div id="ajax_formmail">
<input
type="text"
id="email"
name="data[email]" size="32">
<input
type="hidden"
name="sendto"
value="youremailaddress@example.com">
<input
type="hidden"
name="redirect"
value="none">
<input
type="hidden"
name="subject"
value="Email list
addition">
<input
type="submit"
name="Submit"
value="Submit"
onclick="ajax_formmail()">
</div>
<div id="ajax_formmail_result">
</div>
</div>
</body>
</html>
The the server component:
<?php
// receive email
address
// return success text
$retval = "Thank you
subscribing to the my Email List.<br>
If you wish to be removed from the
list, please send an email to unsubscribe@example.com with
the word 'REMOVE' in the subject line.";
$ret_error = '<a
onclick="ajax_formmail_clear()"><font
color="red">Please enter a valid value. Click
to continue</font></a>';
if ((isset($_GET['email'])
&& is_valid($_GET['email'],
'email')) ||
(isset($_POST['email'])
&& is_valid($_POST['email'],
'email')))
{
$data
= get_data();
$to
= $data['sendto'];
$subject =
$data['subject'];
$message =
build_message($data);
mail($to,
$subject, $message);
echo $retval;
}else{
echo $ret_error;
}
function
get_data()
{
if (isset($_GET['data']))
{
$data
= $_GET['data'];
}else{
$data
= $_POST['data'];
}
return $data;
}
function
build_message($data)
{
$retval =
"Form data\n";
foreach($data
as $key=>$value)
{
$retval
.= $key .":
". $value ."\n";
}
return $retval;
}
function
is_valid($data,
$type)
{
if (is_null($data))
{
return
FALSE;
}
switch ($type)
{
default:
if
($data !==
'')
{
return
TRUE;
}else{
return
FALSE;
}
break;
}
}
?>