...

View Full Version : E-mailing form results to me



spycat
11-26-2006, 12:47 AM
Ok, this is probably laughingly simple to the experts out here.
Go ahead and laugh, but can ya help me too...please?
Well, from that intro, you have probably gathered that I am not a PHP programmer.
Anyway, here it is:
I found a CAPTCHA script that I really liked. However, the author neglected to include code that would
take the input entered into the form and e-mail them to me.
I went looking around for other form handling scripts that sent the results in order to "piece it together" with the CAPTCHA
script. The thing is, most of these scripts get WAY to involved with their own validating processes and such, so
I can not get any to work.
I have a couple of little "send results to my email" snippets inserted into the code now, but like I said, they are not working.
Can someone please take a look at it and well...just fix it?
I intend to learn PHP, but at this point, I just need the script to work.
Also, it is important that the "Thank You Page": "header("Location: http://www.metroactive.com/contact/thanks.html"); " remain functional.
I would be really grateful for this.
In the code I am providing, I will comment with :"Pieced in" on those parts that are from the 2nd script that sends form responses. All the rest of the code is from the "CAPTCHA" script.

Thanks folks



<?php
session_start();
if (isset($_POST['submit'])) {
// clean and check form inputs including the secure image code
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$phone = trim(strip_tags($_POST['phone']));
$event_title = trim(strip_tags($_POST['event_title']));
$event_date_and_time = trim(strip_tags($_POST['event_date_and_time']));
$event_location = trim(strip_tags($_POST['event_location']));
$event_phone_number = trim(strip_tags($_POST['event_phone_number']));
$event_price = trim(strip_tags($_POST['event_price']));
$event_description = trim(strip_tags($_POST['event_description']));

$secure = strtoupper(trim(strip_tags($_POST['secure'])));
$match = $_SESSION['captcha']; // the code on the image

//Start Pieced in
$MailToAddress = "rik408@yahoo.com";
$MailSubject = "Club Event Submission";
if (!$MailFromAddress) {
$MailFromAddress = "$email";
}
//end pieced in

// input error checking
if ($name=="") {
$err.= "Please provide your name<br/>";
}
if (!$email) {
$err.= "Please provide your email address<br>";
}
if ($email) {
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
$err.= $email. " is not a valid email address.<br/>";
}
}
if ($phone=="") {
$err.= "Please provide your phone number<br/>";
}

if ($event_title=="") {
$err.= "Please provide the title of the event<br/>";
}

if ($event_date_and_time=="") {
$err.= "Please provide the date and time of the event<br/>";
}

if ($event_location=="") {
$err.= "Please provide the location of the event<br/>";
}

if ($event_phone_number=="") {
$err.= "Please provide a phone number for the venue<br/>";
}

if ($event_price=="") {
$err.= "Please provide the price to attend the event<br/>";
}

if ($event_description=="") {
$err.= "Please provide a description for the event<br/>";
}




if (!$secure) {
$err.= "No security code entered<br/>";
}
if (($secure!=$match) && ($secure!="")) {
$err.= "Security code mismatch<br/>";
}
if ($err=="") {

//start pieced in: this may be a major trouble spot, since it is preceeded by another "if" statement
if (!is_array($HTTP_POST_VARS))
return;
reset($HTTP_POST_VARS);
while(list($key, $val) = each($HTTP_POST_VARS)) {
$GLOBALS[$key] = $val;
$val=stripslashes($val);
echo "<b>$key</b> = $val<br>";
$Message .= "$key = $val\n";
}


mail( "$MailToAddress", "$MailSubject", "$Message", "From: $MailFromAddress");


header("Location: http://www.metroactive.com/contact/thanks.html");
//end pieced in
exit();
}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Trolls go away</title>
<style type="text/css">
body,td {
font-family:arial, helvetica, sans-serif;
background:#fff;
color:#000;
font-size:12px;
}
input, textarea {
background:#eee;
color:#000;
font-size:12px;
border:1px solid #000;
}
</style>
</head>
<body>
<?php
if ($err!="") {
echo "<strong>Form Error(s)</strong><br/>";
echo "<font color='#cc3300'>". nl2br($err). "</font><br/>";
}
?>

<form name="captcha" method="post" action="<?php echo $_SERVER['SCRIPT_NAME'];?>">
<table cellpadding="3" cellspacing="2" style="border:1px dotted #667;">
<tr>
<td>Name:</td><td><input type="text" name="name" value="<?php echo $_POST['name'];?>"/></td>
</tr>
<tr>
<td>Email:</td><td><input type="text" name="email" value="<?php echo $_POST['email'];?>"/></td>
</tr>
<tr>
<td>Daytime Phone: </td>
<td><input type="text" name="phone" value="<?php echo $_POST['phone'];?>"/></td>
</tr>
<tr>
<td>Club Event Title:</td><td><input type="text" name="event_title" value="<?php echo $_POST['event_title'];?>"/></td>
</tr>
<tr>
<td>Date and Time:</td><td><input type="text" name="event_date_and_time" value="<?php echo $_POST['event_date_and_time'];?>"/></td>
</tr>
<tr>
<td>Location:</td><td><input type="text" name="event_location" value="<?php echo $_POST['event_location'];?>"/></td>
</tr>
<tr>
<td>Venue Phone Number:</td><td><input type="text" name="event_phone_number" value="<?php echo $_POST['event_phone_number'];?>"/></td>
</tr>
<tr>
<td>Price</td><td><input type="text" name="event_price" value="<?php echo $_POST['event_price'];?>"/></td>
</tr>
<tr>
<td valign="top">Event Description:</td><td><textarea rows="5" columns="30" name="event_description"><?php echo $_POST['event_description'];?></textarea></td>
</tr>
<tr>

<td>Security Code</td><td><input type="text" name="secure"/></td>
</tr>
<tr>
<td><img src="captcha_image.php" alt="security image" border="0"/></td><td>
<input type="submit" name="submit" value="Send"/></td>
</tr>
</table>
</form>
</body>
</html>

meth
11-26-2006, 05:32 AM
Providing $_SESSION['captcha'] is set before this form submits, it'll be error free:



<?php
session_start();

//PAGE VARS
$err = '';
$Message = '';

//FORM PROCESSING
if (isset($_POST['submit'])) {
// clean and check form inputs including the secure image code
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$phone = trim(strip_tags($_POST['phone']));
$event_title = trim(strip_tags($_POST['event_title']));
$event_date_and_time = trim(strip_tags($_POST['event_date_and_time']));
$event_location = trim(strip_tags($_POST['event_location']));
$event_phone_number = trim(strip_tags($_POST['event_phone_number']));
$event_price = trim(strip_tags($_POST['event_price']));
$event_description = trim(strip_tags($_POST['event_description']));

$secure = strtoupper(trim(strip_tags($_POST['secure'])));
$match = $_SESSION['captcha']; // the code on the image

// input error checking
if ($name=="") {
$err.= "Please provide your name<br/>";
}
if (!$email) {
$err.= "Please provide your email address<br>";
}
if ($email) {
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
$err.= $email. " is not a valid email address.<br/>";
}
}
if ($phone=="") {
$err.= "Please provide your phone number<br/>";
}

if ($event_title=="") {
$err.= "Please provide the title of the event<br/>";
}

if ($event_date_and_time=="") {
$err.= "Please provide the date and time of the event<br/>";
}

if ($event_location=="") {
$err.= "Please provide the location of the event<br/>";
}

if ($event_phone_number=="") {
$err.= "Please provide a phone number for the venue<br/>";
}

if ($event_price=="") {
$err.= "Please provide the price to attend the event<br/>";
}

if ($event_description=="") {
$err.= "Please provide a description for the event<br/>";
}



if (!$secure) {
$err.= "No security code entered<br/>";
}
if (($secure!=$match) && ($secure!="")) {
$err.= "Security code mismatch<br/>";
}

//if error free
if ($err=="") {

//Start Pieced in
$MailToAddress = "rik408@yahoo.com";
$MailSubject = "Club Event Submission";
$MailFromAddress = ( isset($email) && $email != '') ? $email : 'noReply@myDomain.com';
//end pieced in

//start pieced in: this may be a major trouble spot, since it is preceeded by another "if" statement
if (!is_array($HTTP_POST_VARS))
return;
reset($HTTP_POST_VARS);
while(list($key, $val) = each($HTTP_POST_VARS)) {
$GLOBALS[$key] = $val;
$val=stripslashes($val);
$Message .= "$key = $val\n";
}


mail( "$MailToAddress", "$MailSubject", "$Message", "From: $MailFromAddress");


header("Location: http://www.metroactive.com/contact/thanks.html");
//end pieced in
exit();
}//end if error free

}// end if submit

//PAGE PROCESSING



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Trolls go away</title>
<style type="text/css">
body,td {
font-family:arial, helvetica, sans-serif;
background:#fff;
color:#000;
font-size:12px;
}
input, textarea {
background:#eee;
color:#000;
font-size:12px;
border:1px solid #000;
}
</style>
</head>
<body>
<?php
if ($err!="") {
echo "<strong>Form Error(s)</strong><br/>";
echo "<font color='#cc3300'>". nl2br($err). "</font><br/>";
}
?>

<form name="captcha" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table cellpadding="3" cellspacing="2" style="border:1px dotted #667;">
<tr>
<td>Name:</td><td><input type="text" name="name" value="<?php if(isset($_POST['name']))echo $_POST['name'];?>"/></td>
</tr>
<tr>
<td>Email:</td><td><input type="text" name="email" value="<?php if(isset($_POST['email']))echo $_POST['email'];?>"/></td>
</tr>
<tr>
<td>Daytime Phone: </td>
<td><input type="text" name="phone" value="<?php if(isset($_POST['phone']))echo $_POST['phone'];?>"/></td>
</tr>
<tr>
<td>Club Event Title:</td><td><input type="text" name="event_title" value="<?php if(isset($_POST['event_title']))echo $_POST['event_title'];?>"/></td>
</tr>
<tr>
<td>Date and Time:</td><td><input type="text" name="event_date_and_time" value="<?php if(isset($_POST['event_date_and_time']))echo $_POST['event_date_and_time'];?>"/></td>
</tr>
<tr>
<td>Location:</td><td><input type="text" name="event_location" value="<?php if(isset($_POST['event_location']))echo $_POST['event_location'];?>"/></td>
</tr>
<tr>
<td>Venue Phone Number:</td><td><input type="text" name="event_phone_number" value="<?php if(isset($_POST['event_phone_number']))echo $_POST['event_phone_number'];?>"/></td>
</tr>
<tr>
<td>Price</td><td><input type="text" name="event_price" value="<?php if(isset($_POST['event_price']))echo $_POST['event_price'];?>"/></td>
</tr>
<tr>
<td valign="top">Event Description:</td><td><textarea rows="5" columns="30" name="event_description"><?php if(isset($_POST['event_description']))echo $_POST['event_description'];?></textarea></td>
</tr>
<tr>

<td>Security Code</td><td><input type="text" name="secure"/></td>
</tr>
<tr>
<td><img src="captcha_image.php" alt="security image" border="0"/></td><td>
<input type="submit" name="submit" value="Send"/></td>
</tr>
</table>
</form>
</body>
</html>


Tested without $secure != $match and all works fine.

Basically your script is what I like to call 'lazy' coding. If display_errors == E_ALL in php.ini, you would have seen an abundance of warnings and notices. Any script which ends with headers() AND produces errors is doomed to failure; yours also echo's the form values before the call to headers() - a big no-no.

spycat
11-26-2006, 03:25 PM
Hey Meth,

I wanted to thank you sooo much for taking the time to fix that script for me -- you have my sincere gratitude :)
The way I have the form, it also sends the $key / $val inputs for the secutity code entered and "Submit", but that is a minor thing.
Again, thanks much for your help.

=^ Spycat ^=



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum