jmz1 Posted June 11, 2010 Share Posted June 11, 2010 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 = "hy@howdy.com"; $email_subject = "Request for Information"; function died($error) { // your 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.<br /><br />"; echo $error."<br /><br />"; echo "Please go back and fix these errors.<br /><br />"; 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']) || !isset($_POST['infopackage']) || !isset($_POST['wholesale']) || !isset($_POST['monthlyspecial']) || !isset($_POST['couponcode'])) { 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 $infopackage = $_POST['infopackage']; // not required $wholesale = $_POST['wholesale']; // not required $monthlyspecial = $_POST['monthlyspecial']; // not required $couponcode = $_POST['couponcode']; // not required $error_message = ""; $email_exp = "^[A-Z0-9._%-]+@[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.<br />'; } $string_exp = "^[a-z .'-]+$"; if(!eregi($string_exp,$first_name)) { $error_message .= 'The First Name you entered does not appear to be valid.<br />'; } if(!eregi($string_exp,$last_name)) { $error_message .= 'The Last Name you entered does not appear to be valid.<br />'; } if(!eregi($string_exp,$street_address)) { $error_message .= 'The Street Address you entered does not appear to be valid.<br />'; } if(!eregi($string_exp,$city)) { $error_message .= 'The City you entered does not appear to be valid.<br />'; } if(strlen($zip) < 5) { $error_message .= 'The Zip Code you entered do not appear to be valid.<br />'; } 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"; $email_message .= $infopackage."\n".$wholesale."\n".$monthlyspecial."\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); ?> <!-- include your own success html here --> Thank you for contacting us. We will be in touch with you very soon. <? } ?> Any help would be awesome. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/204514-simple-e-mail-form-help/ Share on other sites More sharing options...
jmz1 Posted June 14, 2010 Author Share Posted June 14, 2010 An entire weekend and no replies? Is this problem harder than I thought...? Quote Link to comment https://forums.phpfreaks.com/topic/204514-simple-e-mail-form-help/#findComment-1071845 Share on other sites More sharing options...
plutomed Posted June 14, 2010 Share Posted June 14, 2010 Do you get any php errors? Quote Link to comment https://forums.phpfreaks.com/topic/204514-simple-e-mail-form-help/#findComment-1071849 Share on other sites More sharing options...
jmz1 Posted June 14, 2010 Author Share Posted June 14, 2010 No, it's not throwing an error. It displays by error handling messages, but no errors even though I've got this to list them: echo $error."<br /><br />"; But the fact that checking all the boxes make the code work correctly leads me to believe that the code is expecting those variables to have something in them when in real use they may and may not. Quote Link to comment https://forums.phpfreaks.com/topic/204514-simple-e-mail-form-help/#findComment-1071859 Share on other sites More sharing options...
New Coder Posted June 14, 2010 Share Posted June 14, 2010 Are your checkboxes these: infopackage, wholesale, monthlyspecial and couponcode and are you recieving this message if the checkboxes are empty?: We are sorry, but there appears to be a problem with the form yoursubmitted. Quote Link to comment https://forums.phpfreaks.com/topic/204514-simple-e-mail-form-help/#findComment-1071862 Share on other sites More sharing options...
jmz1 Posted June 14, 2010 Author Share Posted June 14, 2010 Yes, you're right... those are the checkboxes, and it displays: "We are very sorry, but there were error(s) found with the form your submitted. These errors appear below. We are sorry, but there appears to be a problem with the form your submitted. Please go back and fix these errors." When not all the boxes are checked. Quote Link to comment https://forums.phpfreaks.com/topic/204514-simple-e-mail-form-help/#findComment-1071863 Share on other sites More sharing options...
plutomed Posted June 14, 2010 Share Posted June 14, 2010 I think your problem might be with these. !isset($_POST['infopackage']) || !isset($_POST['wholesale']) || !isset($_POST['monthlyspecial']) || !isset($_POST['couponcode'])) Because if a checkbox is blank it doesn't set a variable so !isset($_POST['couponcode']) will return true. Which should throw the function died(); Why are you checking is these have been set if not if they are no required? Quote Link to comment https://forums.phpfreaks.com/topic/204514-simple-e-mail-form-help/#findComment-1071864 Share on other sites More sharing options...
New Coder Posted June 14, 2010 Share Posted June 14, 2010 Yep what he said, Beat me to it. Quote Link to comment https://forums.phpfreaks.com/topic/204514-simple-e-mail-form-help/#findComment-1071865 Share on other sites More sharing options...
jmz1 Posted June 14, 2010 Author Share Posted June 14, 2010 Good question. Yes, I didn't have that before, but got this: "Notice: Undefined index: infopackage in /home/wolfeclinic2/www/thewolfeclinic.com/send_form_email.php on line 36 Notice: Undefined index: monthlyspecial in /home/wolfeclinic2/www/thewolfeclinic.com/send_form_email.php on line 38 Notice: Undefined index: couponcode in /home/wolfeclinic2/www/thewolfeclinic.com/send_form_email.php on line 39 Thank you for contacting us. We will be in touch with you very soon." The lines are here: $infopackage = $_POST['infopackage']; // not required $wholesale = $_POST['wholesale']; // not required <------ (I had this box checked that time, so that line didn't throw an error) $monthlyspecial = $_POST['monthlyspecial']; // not required $couponcode = $_POST['couponcode']; // not required So I figured the if..then statement was required even for the non-essential variables. Of course, this seems to be a step in the right direction. I got the e-mail. Quote Link to comment https://forums.phpfreaks.com/topic/204514-simple-e-mail-form-help/#findComment-1071868 Share on other sites More sharing options...
plutomed Posted June 14, 2010 Share Posted June 14, 2010 if(isset($_POST['infopackage'])) $infopackage = $_POST['infopackage']; // not required And the same for the other 3. Quote Link to comment https://forums.phpfreaks.com/topic/204514-simple-e-mail-form-help/#findComment-1071873 Share on other sites More sharing options...
jmz1 Posted June 14, 2010 Author Share Posted June 14, 2010 Cool. Thanks a lot for the help, guys. Now it throws: Notice: Undefined variable: infopackage in /home/wolfeclinic2/www/thewolfeclinic.com/send_form_email.php on line 82 Notice: Undefined variable: wholesale in /home/wolfeclinic2/www/thewolfeclinic.com/send_form_email.php on line 82 Notice: Undefined variable: monthlyspecial in /home/wolfeclinic2/www/thewolfeclinic.com/send_form_email.php on line 82 Notice: Undefined variable: couponcode in /home/wolfeclinic2/www/thewolfeclinic.com/send_form_email.php on line 82 Line 82 is this: $email_message .= $infopackage."\n\n".$wholesale."\n\n".$monthlyspecial."\n\n".$couponcode; Same deal though, if the box is checked then the error doesn't list that variable, but even so I'm still receiving the e-mail that the form sends. Quote Link to comment https://forums.phpfreaks.com/topic/204514-simple-e-mail-form-help/#findComment-1071879 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.