Page 1 of 1

want to limit when php form sends email

Posted: Thu Mar 30, 2006 12:48 pm
by anarchist.babe
PHP newbie here! I've put together a simple PHP form that collects values; checks that all of the required values have been filled out; and sends an email both to my company and the visitor filling out the form.

The issue I'm running into is that the form sends both emails when the values aren't met. That is, if a visitor doesn't fill in the 'name' field, he's fed a message to go back to the form and fill out that field... but the incomplete form still gets sent to me and him. That will clog up my company's inbox by potentially sending 2 or 3 emails from the same visitor, and force us to sort through the complete vs incomplete forms.

My research shows that maybe I need to add in some Javascript to stop this from happening. But I'm not sure (and am hoping that's not the solution!). I would truly appreciate any guidance.

Here's the code for the page that receives the values:

Code: Select all

<?php
  
if(!$email == "" && (!strstr($email,"@") || !strstr($email,".")))
{

echo "<h3>Sorry, but the form cannot be sent until the fields marked with a red X are completed. Your email address is not filled out correctly. Please return to the form and fix your email address. Your Request has not been submitted yet. Thanks!</h3>\n";
  echo "<a href='javascript:history.back(1);'>back to form</a>";
}

if(empty($name) || empty($last) || empty($product) || empty($explain )) {
echo "<h3>Sorry, but the form cannot be sent until the fields marked with a red X are completed. Please return to the form and fill in those fields. Thanks!</h3>\n";
  echo "<a href='javascript:history.back(1);'>back to form</a>";
}


$todayis = date("l, F j, Y, g:i a") ;

$subject = $topic;

$explain = stripcslashes($explain);


$message = " $todayis [EST] \n
First name: $name \n
Last name: $last \n
Company: $company \n
Email: $email \n
Phone: $phone \n
Product: $product \n
Platform: $platform \n
Regarding: $topic \n
Message: $explain \n
Additional Info : IP = $ip \n
Browser Info: $httpagent \n
Referral : $httpref \n
";

$from = "From: $email\r\n";


mail("me@da.com", $subject, $message, $from);

?>
  
</div>
<p align="left">

Date: <?php echo $todayis ?>
<br />
<br />

Thank you, <?php echo $name, '&nbsp;', $last ?>, 
 for using our online request form.    <br/><br />
A copy of your message has already been mailed to you. <br />
Please check the email and let us know of any changes or missing information as soon as possible.<br />
Thank you! - the Anarchists <br />
  <br />
  Your message is about: <?php echo $topic ?>
  <br />
  <br/>
  Your message reads:<br />
  
<?php $explainout = str_replace("\r", "<br/>", $explain);
echo $explainout; 

mail("$email", "Thank you for contacting Digital Anarchy", $message, "From: Digital Anarchy <me@da.com>");

?>

Posted: Thu Mar 30, 2006 12:51 pm
by s.dot
if(!$email == ""

i think should be, if ($email == ""

Re: want to limit when php form sends email

Posted: Thu Mar 30, 2006 1:00 pm
by Benjamin
Here is a quick fix.

Code: Select all

<?php
 
$Error = false;
if(!$email == "" && (!strstr($email,"@") || !strstr($email,".")))
{

echo "<h3>Sorry, but the form cannot be sent until the fields marked with a red X are completed. Your email address is not filled out correctly. Please return to the form and fix your email address. Your Request has not been submitted yet. Thanks!</h3>\n";
  echo "<a href='javascript:history.back(1);'>back to form</a>";
$Error = true;
}

if(empty($name) || empty($last) || empty($product) || empty($explain )) {
echo "<h3>Sorry, but the form cannot be sent until the fields marked with a red X are completed. Please return to the form and fill in those fields. Thanks!</h3>\n";
  echo "<a href='javascript:history.back(1);'>back to form</a>";
$Error = true;
}


$todayis = date("l, F j, Y, g:i a") ;

$subject = $topic;

$explain = stripcslashes($explain);


$message = " $todayis [EST] \n
First name: $name \n
Last name: $last \n
Company: $company \n
Email: $email \n
Phone: $phone \n
Product: $product \n
Platform: $platform \n
Regarding: $topic \n
Message: $explain \n
Additional Info : IP = $ip \n
Browser Info: $httpagent \n
Referral : $httpref \n
";

$from = "From: $email\r\n";

if ($Error != 'true') {
  mail("me@da.com", $subject, $message, $from);
}

?>
  
</div>
<p align="left">

Date: <?php echo $todayis ?>
<br />
<br />

Thank you, <?php echo $name, '&nbsp;', $last ?>, 
 for using our online request form.    <br/><br />
A copy of your message has already been mailed to you. <br />
Please check the email and let us know of any changes or missing information as soon as possible.<br />
Thank you! - the Anarchists <br />
  <br />
  Your message is about: <?php echo $topic ?>
  <br />
  <br/>
  Your message reads:<br />
  
<?php $explainout = str_replace("\r", "<br/>", $explain);
echo $explainout; 

if ($Error != 'true') {
mail("$email", "Thank you for contacting Digital Anarchy", $message, "From: Digital Anarchy <me@da.com>");
}
?>

Posted: Thu Mar 30, 2006 8:28 pm
by anarchist.babe
wow, thanks for both replies, and to agtlewis for the code. I would have had great difficulty writing that myself, and will be sure to read about what you wrote so i can learn from this. The script works perfectly. One last question: What keywords would you have used in searching for this info in PHP forums and info sites? I did quite a bit of poking around before i posted... Thanks again! (whew!)