Hi guys, im very new to php and have been trying to do up a html email a friend script for placement on a clients website.
Could someone have a look at the code and tell me what im missing plz?? very frustrating…..
The html form –
<html>
<title>Test script</title>
<head>
<style>
#errors
{
font-size:14px;
font-weight:normal;
margin:10px;
padding:10px;
}
</style>
<script type=”text/javascript” src=”prototype.js”></script>
<script type=”text/javascript”>
function sendRequest() {
new Ajax.Request(“email_form_process.php”, {
method: ‘post’,
postBody: “friend=”+$F(“friend”)+”&email=”+$F(“email”)+”&you=”+$F(“you”),
onComplete: showResponse
});
}
function showResponse(req)
{
$(‘errors’).style.borderStyle= “solid”;
var res=/message was received/;
if(req.responseText.match(res))
{
$(‘errors’).style.borderColor= “green”;
$(‘errors’).style.color=”green”;
}else{
$(‘errors’).style.borderColor= “red”;
$(‘errors’).style.color=”red”;
}
$(‘errors’).style.borderSize= “1px”;
$(‘errors’).innerHTML= req.responseText;
}
</script>
</head>
<body>
<form id=”test” method=”post” onSubmit=”return false;”>
<table border=”0″>
<tr>
<td colspan=”2″>
<div id=”errors”>
</div>
</td>
</tr>
<tr>
<td colspan=”2″>
<font size=”+2″><b>Email Form</b></font>
</td>
</tr>
<tr>
<td>
<b>Your Name:</b>
</td>
<td>
<input type=”text” name=”you” id=”you” size=”55″ maxlength=”20″>
</td>
</tr>
<tr>
<td>
<b>Friend’s Name:</b>
</td>
<td>
<input type=”text” name=”friend” id=”friend” size=”55″ maxlength=”20″>
</td>
</tr>
<tr>
<td>
<b>Email:</b>
</td>
<td>
<input type=”text” name=”email” id=”email” size=”55″ maxlength=”35″>
</td>
</tr>
<tr>
<td colspan=”2″ align=”right”>
<input type=”submit” value=”submit” name=”submit” onClick=”javascript: sendRequest();”>
</td>
</tr>
</table>
</form>
</body>
</html>
PHP file –
<?php
$from = $_POST[“you”];
$to = $_POST[“email”];
$subject = “php email script”;
$message = ‘
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
‘;
function validate_form_items()
{
global $msg_min_chars, $msg_max_chars;
$msg_chars = “{“.$msg_min_chars.”,”.$msg_max_chars.”}”;
$form_items = array(
“you” => array(
“regex” => “/^([a-zA-Z ‘-]+)$/”,
“error” => “Name appears to be in inproper format”,
),
“friend” => array(
“regex” => “/^([a-zA-Z ‘-]+)$/”,
“error” => “Name appears to be in inproper format”,
),
“email” => array(
“regex” =>
“/^[A-Za-z0-9](([_.-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([.-]?[a-zA-Z0-9]+)*).([A-Za-z]{2,})$/”,
“error” => “Please enter your valid email address”,
),
);
$errors = array();
foreach($form_items as $item_name => $item_props)
{
if (!preg_match($item_props[“regex”], trim($_POST[$item_name])))
{
$errors[] = $item_props[“error”];
}
}
return $errors;
}
function email( $to, $subject, $message)
{
$headers = “From: “.$from.”rn”;
$headers .= “Reply-To: “.$from.”rn”;
$headers .= “Return-Path: “.$from.”rn”;
$headers = ‘MIME-Version: 1.0’ . “rn”;
$headers .= ‘Content-type: text/html; charset=iso-8859-1’ . “rn”;
if (mail($to,$subject,$message,$headers) ) {
echo “Thanks for your inquiry. Your message was received. <br>We will get back to your shortly.”;
} else {
echo “Your message could not be sent at this time. Please try again.”;
}
}
function print_error($errors)
{
foreach($errors as $error)
{
echo $error.”<br>”;
}
}
function form_process()
{
global $to, $subject;
$errors = validate_form_items();
if(count($errors) == 0)
{
email($to, $subject, $message, $headers);
}
print_error($errors);
}
form_process();
?>
Any help would be most appreciated