E-mail form problem

Hi guys. I’m really rusty with PHP, and I’ve spent my day today trying to work up a fairly simple e-mail form using PHP.

I’ve got the form working except for four check boxes, which I need to be optional unlike the rest of the form which is required, but I can’t quite get it right.

When i fill out the form and check all four of the boxes, than the code works as it should and sends an e-mail to me. If I’m missing one or more of the boxes, though, the code dies somewhere before the main error handling section.

So evidently the PHP is expecting the checkbox variables to contain something, even though I want it to be optional, and it throws an error. I’m not familiar enough with PHP syntax and functions to know what to do.

Here it is (This is probably obvious, but as you can see I’ve put the PHP on a separate page and just include it on the form itself. If you need to see the form’s HTML, I can include it but I didn’t think it would be needed):


<?php if(isset($_POST['email'])) { $email_to = "[email protected]"; $email_subject = "Request for Information"; function died($error) { // error code can go here echo "We are very sorry, but there were error(s) found with the form your submitted. "; echo "These errors appear below.

"; echo $error."

"; echo "Please go back and fix these errors.

"; die(); } // validation expected data exists if(!isset($_POST['first_name']) || !isset($_POST['last_name']) || !isset($_POST['email']) || !isset($_POST['street_address']) || !isset($_POST['city']) || !isset($_POST['zip'])) { died('We are sorry, but there appears to be a problem with the form your submitted.'); } $first_name = $_POST['first_name']; // required $last_name = $_POST['last_name']; // required $email_from = $_POST['email']; // required $street_address = $_POST['street_address']; // required $city = $_POST['city']; // required $zip = $_POST['zip']; // required if(isset($_POST['infopackage'])) $infopackage = $_POST['infopackage']; // not required if(isset($_POST['wholesale'])) $wholesale = $_POST['wholesale']; // not required if(isset($_POST['monthlyspecial'])) $monthlyspecial = $_POST['monthlyspecial']; // not required if(isset($_POST['couponcode'])) $couponcode = $_POST['couponcode']; // not required $error_message = ""; $email_exp = "^[A-Z0-9._%-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}$"; if(!eregi($email_exp,$email_from)) { $error_message .= 'The Email Address you entered does not appear to be valid.
'; } $string_exp = "^[a-z .'-]+$"; if(!eregi($string_exp,$first_name)) { $error_message .= 'The First Name you entered does not appear to be valid.'; } if(!eregi($string_exp,$last_name)) { $error_message .= 'The Last Name you entered does not appear to be valid.
'; } if(!eregi($string_exp,$street_address)) { $error_message .= 'The Street Address you entered does not appear to be valid.
'; } if(!eregi($string_exp,$city)) { $error_message .= 'The City you entered does not appear to be valid.
'; } if(strlen($zip) < 5) { $error_message .= 'The Zip Code you entered do not appear to be valid.
'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "First Name: ".clean_string($first_name)."\n"; $email_message .= "Last Name: ".clean_string($last_name)."\n"; $email_message .= "Email: ".clean_string($email_from)."\n"; $email_message .= "Street Address: ".clean_string($street_address)."\n"; $email_message .= "City: ".clean_string($city)."\n\n"; $email_message .= "I would like to receive: "."\n\n"; $email_message .= $infopackage."\n\n".$wholesale."\n\n".$monthlyspecial."\n\n".$couponcode; // create email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); ?>

Thank you for contacting us. We will be in touch with you very soon.

<? } ?>

[/php]$infopackage, $wholesale, $monthlyspecial, and $couponcode are my checkbox variables. This script almost works perfectly (I even get the e-mail and it looks fine), but I always get this error for each checkbox that isn’t check:

Notice: Undefined variable: [checkbox variable name] in /home/wolfeclinic2/www/thewolfeclinic.com/send_form_email.php on line 82

Line 82 is this:

[php]$email_message .= $infopackage."\n\n".$wholesale."\n\n".$monthlyspecial."\n\n".$couponcode;[/php]
So evidently the script is expecting those variables to have something in them, but they won’t always. I want them to be optional. What am I missing…? I have a feeling this is really simple, but I just can’t finish it…


This is just a notice, not really error. You can turn notices Off using error_reporting() or in your php.ini.
BUT if you wish to produce valid code with no such notices, you just need to define these variables, which you are setting when checkboxes are checked. For example, this line in your code:

if(isset($_POST[‘infopackage’])) $infopackage = $_POST[‘infopackage’];

need to be modified to this:
if(isset($_POST[‘infopackage’])) $infopackage = $_POST[‘infopackage’]; else $infopackage = ‘’;

And so on with all 4 variables related to checkboxes. See, if you not check the checkbox, the array element $_POST[‘infopackage’] is not set - function isset() return false. And therefore variable $infopackage need to be defined in case of unchecked checkbox too - we’re setting it to empty string to avoid notices.

Thanks a lot for the help. Yesterday I tried doing that myself and ended up with this code:

[php]$infopackage = “x”;
$wholesale = “x”;
$monthlyspecial = “x”;
$couponcode = “x”;

if(isset($_POST[‘infopackage’])) $infopackage = $_POST[‘infopackage’];
if(isset($_POST[‘wholesale’])) $wholesale = $_POST[‘wholesale’];
if(isset($_POST[‘monthlyspecial’])) $monthlyspecial = $_POST[‘monthlyspecial’];
if(isset($_POST[‘couponcode’])) $couponcode = $_POST[‘couponcode’];[/php]

Mostly because I couldn’t figure out how to use an if…then…else statement like the one you used. :slight_smile: So we’re good to go. Thanks again!

Sponsor our Newsletter | Privacy Policy | Terms of Service