random1739 Posted July 17, 2007 Share Posted July 17, 2007 Hi, I'm quite new to PHP, and I'd like to make a contact page for a website where the email field is mandatory. I've used Smarty to make the page, and at the moment it works fine and displays fine. The few code edits I've manually made don't appear to work. I'd like to redirect the user to an error page if they forget to include their return email address. Here's the original, working code as it stands: <?php require_once('libs/Smarty.class.php'); $smarty = new Smarty; $smarty->compile_check = true; $smarty->debugging = false; if (isset($_POST['action']) && $_POST['action'] == 'Submit request') { $body = ''; $body .= 'How did you hear about us? ' . $_POST['heard'] . "\n"; $body .= "\n"; $body .= '* Your adventure' . "\n"; $body .= 'Which destination are you interested in? ' . $_POST['holiday'] . "\n"; $body .= 'When do you intend to travel? ' . $_POST['month'] . ' ' . $_POST['year'] . "\n"; $body .= 'What type of diving do you enjoy doing? ' . $_POST['diving_type'] . "\n"; $body .= 'Number of guests: ' . "\n"; $body .= 'Adults: ' . $_POST['adults'] . ' (' . $_POST['adult_divers'] . ' are divers)' . "\n"; $body .= 'Children: ' . $_POST['children'] . ' (' . $_POST['child_divers'] . ' are divers)' . "\n"; $body .= 'Infants: ' . $_POST['infants'] . ' (' . $_POST['infant_divers'] . ' are divers)' . "\n"; $body .= 'Comments: ' . "\n"; $body .= $_POST['comments'] . '.' . "\n"; $body .= "\n"; $body .= '* Your details' . "\n"; $body .= 'Name ' . $_POST['name'] . "\n"; $body .= 'Email ' . $_POST['email'] . "\n"; $body .= 'Address ' . $_POST['address'] . "\n"; $body .= 'City ' . $_POST['city'] . "\n"; $body .= 'Postcode ' . $_POST['postcode'] . "\n"; $body .= 'State ' . $_POST['state'] . "\n"; $body .= 'Country ' . $_POST['country'] . "\n"; $body .= 'Contact by telephone? ' . ($_POST['contactbytelephone'] ? 'Yes' : 'No' ) . "\n"; $body .= 'Telephone number ' . $_POST['phone'] . "\n"; $body .= 'Monthly newsletter? ' . ($_POST['receivenewsletter'] ? 'Yes' : 'No' ) . "\n"; //echo str_replace("\n", '<br>', $body); $to = '*****@optusnet.com.au'; $from = "Website<*****@optusnet.com.au>"; $subject = "Holiday information - From website"; $headers = "From: $from"; mail($to, $subject, $body, $headers); $smarty->assign('submit', 1); } smarty_constants(&$smarty); $smarty->display('head.tpl'); $smarty->display('contact.tpl'); $smarty->display('tail.tpl'); function smarty_constants(&$smarty) { $HEARDS = array ( 'The Internet' => 'The Internet', 'Word of Mouth' => 'Word of Mouth', 'Business reference' => 'Business reference', 'Advertisement' => 'Advertisement', 'Magazine' => 'Magazine', 'Other' => 'Other' ); $smarty->assign('heards', $HEARDS); $HOLIDAYS = array ( 'Australia / Great Barrier Reef' => 'Australia / Great Barrier Reef', 'Cocos (Keeling) / Christmas Islands' => 'Cocos (Keeling) / Christmas Islands', 'Cook Islands' => 'Cook Islands', 'East Timor' => 'East Timor', 'Fiji' => 'Fiji', 'Indonesia' => 'Indonesia', 'Liveaboards' => 'Liveaboards', 'Malaysia' => 'Malaysia', 'Maldives' => 'Maldives', 'Marshall Islands' => 'Marshall Islands', 'Micronesia' => 'Micronesia', 'Niue' => 'Niue', 'Papua New Guinea' => 'Papua New Guinea', 'Philippines' => 'Philippines', 'Red Sea / Egypt' => 'Red Sea / Egypt', 'Solomon Islands' => 'Solomon Islands', 'Tahiti' => 'Tahiti', 'Tonga' => 'Tonga', 'Vanuatu' => 'Vanuatu', 'Western Samoa' => 'Western Samoa', 'Other (Refer to comments)' => 'Other (Refer to comments)' ); $smarty->assign('holidays', $HOLIDAYS); $DIVING_TYPES = array ( 'Wreck' => 'Wreck', 'Cave' => 'Cave', 'Reef' => 'Reef', 'Wall' => 'Wall', 'Temperate Water' => 'Temperate Water', 'Tropical' => 'Tropical', 'Deep' => 'Deep', 'Drift' => 'Drift', 'Exploratory' => 'Exploratory', 'Live-aboards' => 'Live-aboards', 'Technical Diving' => 'Technical Diving', 'Mixed Gases' => 'Mixed Gases' ); $smarty->assign('diving_types', $DIVING_TYPES); $MONTHS = array ( 'January' => 'January', 'February' => 'February', 'March' => 'March', 'April' => 'April', 'May' => 'May', 'June' => 'June', 'July' => 'July', 'August' => 'August', 'September' => 'September', 'October' => 'October', 'November' => 'November', 'December' => 'December' ); $smarty->assign('months', $MONTHS); $YEARS = array ( '2007' => '2007', '2008' => '2008', '2009' => '2009', '2010' => '2010', ); $smarty->assign('years', $YEARS); return; } ?> Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/60296-mandatory-email-field-contact-form/ Share on other sites More sharing options...
dbillings Posted July 17, 2007 Share Posted July 17, 2007 Try this (also include the rest of your code with my few changes.) if (isset($_POST['action'],$_POST['email']) && $_POST['action'] == 'Submit request') { $body = ''; $body .= 'How did you hear about us? ' . $_POST['heard'] . "\n"; $body .= "\n"; $body .= '* Your adventure' . "\n"; $body .= 'Which destination are you interested in? ' . $_POST['holiday'] . "\n"; $body .= 'When do you intend to travel? ' . $_POST['month'] . ' ' . $_POST['year'] . "\n"; $body .= 'What type of diving do you enjoy doing? ' . $_POST['diving_type'] . "\n"; $body .= 'Number of guests: ' . "\n"; $body .= 'Adults: ' . $_POST['adults'] . ' (' . $_POST['adult_divers'] . ' are divers)' . "\n"; $body .= 'Children: ' . $_POST['children'] . ' (' . $_POST['child_divers'] . ' are divers)' . "\n"; $body .= 'Infants: ' . $_POST['infants'] . ' (' . $_POST['infant_divers'] . ' are divers)' . "\n"; $body .= 'Comments: ' . "\n"; $body .= $_POST['comments'] . '.' . "\n"; $body .= "\n"; $body .= '* Your details' . "\n"; $body .= 'Name ' . $_POST['name'] . "\n"; $body .= 'Email ' . $_POST['email'] . "\n"; $body .= 'Address ' . $_POST['address'] . "\n"; $body .= 'City ' . $_POST['city'] . "\n"; $body .= 'Postcode ' . $_POST['postcode'] . "\n"; $body .= 'State ' . $_POST['state'] . "\n"; $body .= 'Country ' . $_POST['country'] . "\n"; $body .= 'Contact by telephone? ' . ($_POST['contactbytelephone'] ? 'Yes' : 'No' ) . "\n"; $body .= 'Telephone number ' . $_POST['phone'] . "\n"; $body .= 'Monthly newsletter? ' . ($_POST['receivenewsletter'] ? 'Yes' : 'No' ) . "\n"; //echo str_replace("\n", ' ', $body); $to = '*****@optusnet.com.au'; $from = "Website<*****@optusnet.com.au>"; $subject = "Holiday information - From website"; $headers = "From: $from"; mail($to, $subject, $body, $headers); $smarty->assign('submit', 1); }elseif(!isset($_POST['email'])){ echo "Please enter a valid email address."; } Quote Link to comment https://forums.phpfreaks.com/topic/60296-mandatory-email-field-contact-form/#findComment-299952 Share on other sites More sharing options...
MadTechie Posted July 17, 2007 Share Posted July 17, 2007 try this, its a basic check nothing special! <?php if (isset($_POST['email'])) { header("Location: http:\\whatever.com"); } require_once('libs/Smarty.class.php'); $smarty = new Smarty; $smarty->compile_check = true; $smarty->debugging = false; if (isset($_POST['action']) && $_POST['action'] == 'Submit request') { $body = ''; $body .= 'How d...... Quote Link to comment https://forums.phpfreaks.com/topic/60296-mandatory-email-field-contact-form/#findComment-299953 Share on other sites More sharing options...
random1739 Posted July 17, 2007 Author Share Posted July 17, 2007 Thank you both for your help! I've implemented MadTechie's method as I had an error page setup already, and it seems to be working I'm not sure dbillings method works as I get the "Please enter a valid email address" echo automatically every time the page loads. I also get a success message when I send a form without an email (which is not what I want). I have yet to make sure the form isn't being sent through (I'm unable to check incoming emails at the moment) Irrespective of this, thank you both for your time and help Quote Link to comment https://forums.phpfreaks.com/topic/60296-mandatory-email-field-contact-form/#findComment-300168 Share on other sites More sharing options...
random1739 Posted July 24, 2007 Author Share Posted July 24, 2007 I've finally had a chance to test the code, but now everyone who tries to submit the contact form receives the "no valid email" message. To top it off, even those with no valid address still get emailed to us ??? Here's the complete code from my contact.php page at the moment (which I amended as per MadTechie's suggestion) <?php if (isset($_POST['email'])) { header("Location: http://www.diveadventures.com.au/pages/no_email.html"); } require_once('libs/Smarty.class.php'); $smarty = new Smarty; $smarty->compile_check = true; $smarty->debugging = false; if (isset($_POST['action']) && $_POST['action'] == 'Submit request') { $body = ''; $body .= 'How did you hear about Dive Adventures? ' . $_POST['heard'] . "\n"; $body .= "\n"; $body .= '* Your adventure' . "\n"; $body .= 'Which destination are you interested in? ' . $_POST['holiday'] . "\n"; $body .= 'When do you intend to travel? ' . $_POST['month'] . ' ' . $_POST['year'] . "\n"; $body .= 'What type of diving do you enjoy doing? ' . $_POST['diving_type'] . "\n"; $body .= 'Number of guests: ' . "\n"; $body .= 'Adults: ' . $_POST['adults'] . ' (' . $_POST['adult_divers'] . ' are divers)' . "\n"; $body .= 'Children: ' . $_POST['children'] . ' (' . $_POST['child_divers'] . ' are divers)' . "\n"; $body .= 'Infants: ' . $_POST['infants'] . ' (' . $_POST['infant_divers'] . ' are divers)' . "\n"; $body .= 'Comments: ' . "\n"; $body .= $_POST['comments'] . '.' . "\n"; $body .= "\n"; $body .= '* Your details' . "\n"; $body .= 'Name ' . $_POST['name'] . "\n"; $body .= 'Email ' . $_POST['email'] . "\n"; $body .= 'Address ' . $_POST['address'] . "\n"; $body .= 'City ' . $_POST['city'] . "\n"; $body .= 'Postcode ' . $_POST['postcode'] . "\n"; $body .= 'State ' . $_POST['state'] . "\n"; $body .= 'Country ' . $_POST['country'] . "\n"; $body .= 'Contact by telephone? ' . ($_POST['contactbytelephone'] ? 'Yes' : 'No' ) . "\n"; $body .= 'Telephone number ' . $_POST['phone'] . "\n"; $body .= 'Monthly newsletter? ' . ($_POST['receivenewsletter'] ? 'Yes' : 'No' ) . "\n"; //echo str_replace("\n", '<br>', $body); $to = 'divesydney@optusnet.com.au'; $from = "diveadventures <sydney@diveadventures.com.au>"; $subject = "Holiday information - From www.diveadventures.com.au"; $headers = "From: $from"; mail($to, $subject, $body, $headers); $smarty->assign('submit', 1); } smarty_constants(&$smarty); $smarty->display('head.tpl'); $smarty->display('contact.tpl'); $smarty->display('tail.tpl'); function smarty_constants(&$smarty) { $HEARDS = array ( 'The Internet' => 'The Internet', 'Word of Mouth' => 'Word of Mouth', 'Business reference' => 'Business reference', 'Advertisement' => 'Advertisement', 'Dive magazine' => 'Dive magazine', 'Other' => 'Other' ); $smarty->assign('heards', $HEARDS); $HOLIDAYS = array ( 'Australia / Great Barrier Reef' => 'Australia / Great Barrier Reef', 'Cocos (Keeling) / Christmas Islands' => 'Cocos (Keeling) / Christmas Islands', 'Cook Islands' => 'Cook Islands', 'East Timor' => 'East Timor', 'Fiji' => 'Fiji', 'Indonesia' => 'Indonesia', 'Liveaboards' => 'Liveaboards', 'Malaysia' => 'Malaysia', 'Maldives' => 'Maldives', 'Marshall Islands' => 'Marshall Islands', 'Micronesia' => 'Micronesia', 'Niue' => 'Niue', 'Papua New Guinea' => 'Papua New Guinea', 'Philippines' => 'Philippines', 'Red Sea / Egypt' => 'Red Sea / Egypt', 'Solomon Islands' => 'Solomon Islands', 'Tahiti' => 'Tahiti', 'Tonga' => 'Tonga', 'Vanuatu' => 'Vanuatu', 'Western Samoa' => 'Western Samoa', 'Other (Refer to comments)' => 'Other (Refer to comments)' ); $smarty->assign('holidays', $HOLIDAYS); $DIVING_TYPES = array ( 'Wreck' => 'Wreck', 'Cave' => 'Cave', 'Reef' => 'Reef', 'Wall' => 'Wall', 'Temperate Water' => 'Temperate Water', 'Tropical' => 'Tropical', 'Deep' => 'Deep', 'Drift' => 'Drift', 'Exploratory' => 'Exploratory', 'Live-aboards' => 'Live-aboards', 'Technical Diving' => 'Technical Diving', 'Mixed Gases' => 'Mixed Gases' ); $smarty->assign('diving_types', $DIVING_TYPES); $MONTHS = array ( 'January' => 'January', 'February' => 'February', 'March' => 'March', 'April' => 'April', 'May' => 'May', 'June' => 'June', 'July' => 'July', 'August' => 'August', 'September' => 'September', 'October' => 'October', 'November' => 'November', 'December' => 'December' ); $smarty->assign('months', $MONTHS); $YEARS = array ( '2007' => '2007', '2008' => '2008', '2009' => '2009', '2010' => '2010', ); $smarty->assign('years', $YEARS); return; } ?> Anyone have any suggestions as to how I might be able to correct this? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/60296-mandatory-email-field-contact-form/#findComment-306046 Share on other sites More sharing options...
MadTechie Posted July 24, 2007 Share Posted July 24, 2007 to redirect when NOT valid use the code below if (!isset($_POST['email'])) { header("Location: http://www.diveadventures.com.au/pages/no_email.html"); } infact heres some improved code, this check the address is a valid address (this does NOT mean the email exists but it is a valid format) if (!isset($_POST['email'])) { header("Location: http://www.diveadventures.com.au/pages/no_email.html"); }else{ if (!preg_match('/\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b/i', $_POST['email'])) { header("Location: http://www.diveadventures.com.au/pages/no_email.html"); } } Quote Link to comment https://forums.phpfreaks.com/topic/60296-mandatory-email-field-contact-form/#findComment-306262 Share on other sites More sharing options...
random1739 Posted July 26, 2007 Author Share Posted July 26, 2007 to redirect when NOT valid use the code below if (!isset($_POST['email'])) { header("Location: http://www.diveadventures.com.au/pages/no_email.html"); } Thanks for your efforts, but something is still causing the form to be submitted, despite testing it without anything in the email address field. Could it be something in this part here? require_once('libs/Smarty.class.php'); $smarty = new Smarty; $smarty->compile_check = true; $smarty->debugging = false; if (isset($_POST['action']) && $_POST['action'] == 'Submit request') Should I be completely replacing the 2nd "if (isset($_POST...." line with something like this? if (!isset($_POST['email'])) { header("Location: http://www.diveadventures.com.au/pages/no_email.html"); }else{ if (isset($_POST['action']) && $_POST['action'] == 'Submit request') } Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/60296-mandatory-email-field-contact-form/#findComment-307725 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.