bobd72 Posted July 26, 2010 Share Posted July 26, 2010 Hello, I have an html form in a php page which sends an email with user entered information after validation tests. It works perfectly in isolation. However, I want to call it with an include() statement inside a div on another page. When I do this the form renders correctly and I can enter the data, but on Submit the Email is not sent. Any assistance would be appreciated Quote Link to comment Share on other sites More sharing options...
joel24 Posted July 26, 2010 Share Posted July 26, 2010 will need to see code Quote Link to comment Share on other sites More sharing options...
bobd72 Posted July 26, 2010 Author Share Posted July 26, 2010 will need to see code Thanks Joel24. This is just test code: Main page: <?php session_start(); function form() { $page = $_GET['page']; switch($page) { default: include 'Image1.html'; break; case "contact": include 'html-contact-form.php'; break; } } ?> <html> <head> <title>Sample</title> </head> <body> <div id="header">Test Page</div> <div id="navigation"><a href="?page=contact">Contact</a> link</div> <div id="content"> <?php form(); ?> </div> </body> </html> Include code: <?php $your_email ='email@address';// <<=== update to your email address session_start(); $errors = ''; $name = ''; $visitor_email = ''; $visitor_website = ''; $user_message = ''; if(isset($_POST['submit'])) { $name = $_POST['name']; $visitor_email = $_POST['email']; $visitor_website = $_POST['website']; $user_message = $_POST['message']; ///------------Do Validations------------- if(empty($name)||empty($visitor_email)) { $errors .= "\n Name and Email are required fields. "; } if(IsInjected($visitor_email)) { $errors .= "\n Bad email value!"; } if(empty($_SESSION['6_letters_code'] ) || strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0) { //Note: the captcha code is compared case insensitively. //if you want case sensitive match, update the check above to // strcmp() $errors .= "\n The captcha code does not match!"; } if(empty($errors)) { //send the email $to = $your_email; $subject="New form submission"; $from = $your_email; $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''; $body = "A user $name submitted the contact form:\n". "Name: $name\n". "Email: $visitor_email \n". "Website Update?: $visitor_website \n". "Message: \n ". "$user_message\n". "IP: $ip\n"; $headers = "From: $from \r\n"; $headers .= "Reply-To: $visitor_email \r\n"; mail($to, $subject, $body,$headers); // header('Location: thank-you.html'); } } // Function to validate against any email injection attempts function IsInjected($str) { $injections = array('(\n+)', '(\r+)', '(\t+)', '(%0A+)', '(%0D+)', '(%08+)', '(%09+)' ); $inject = join('|', $injections); $inject = "/$inject/i"; if(preg_match($inject,$str)) { return true; } else { return false; } } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Contact Us</title> <!-- define some style elements--> <style> label,a, body { font-family : Arial, Helvetica, sans-serif; font-size : 12px; } .err { font-family : Verdana, Helvetica, sans-serif; font-size : 12px; color: red; } </style> <!-- a helper script for vaidating the form--> <script language="JavaScript" src="scripts/gen_validatorv31.js" type="text/javascript"></script> </head> <body> <?php if(!empty($errors)){ echo "<p class='err'>".nl2br($errors)."</p>"; } ?> <div id='contact_form_errorloc' class='err'></div> <form method="POST" name="contact_form" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"> <p> <label for='name'>Name: </label><br> <input type="text" name="name" value='<?php echo htmlentities($name) ?>'> </p> <p> <label for='email'>Email: </label><br> <input type="text" name="email" value='<?php echo htmlentities($visitor_email) ?>'> </p> <p> <label for='website'>Would you like to receive Website Update News? </label><br> <input type="radio" name="website" value='Yes <?php echo htmlentities($visitor_website) ?>' checked>YES <br> <input type="radio" name="website" value='No <?php echo htmlentities($visitor_website) ?>'>NO <br> </p> <p> <label for='message'>Message:</label> <br> <textarea name="message" rows=8 cols=30><?php echo htmlentities($user_message) ?></textarea> </p> <p> <img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br> <label for='message'>Enter the code above here :</label><br> <input id="6_letters_code" name="6_letters_code" type="text"><br> <small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small> </p> <input type="submit" value="Submit" name='submit'> </form> <script language="JavaScript"> var frmvalidator = new Validator("contact_form"); frmvalidator.EnableOnPageErrorDisplaySingleBox(); frmvalidator.EnableMsgsTogether(); frmvalidator.addValidation("name","req","Please provide your name"); frmvalidator.addValidation("email","req","Please provide your email"); frmvalidator.addValidation("email","email","Please enter a valid email address"); </script> <script language='JavaScript' type='text/javascript'> function refreshCaptcha() { var img = document.images['captchaimg']; img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000; } </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
joel24 Posted July 26, 2010 Share Posted July 26, 2010 You're posting the form to the Test-Page html file and the include is not included by default, hence the PHP file is not being included and can't pick up any form posts. You need the form to post to url.com?page=contact, so the PHP file is included and can read the $_POSTs like so. action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>?page=contact"> or in full. <?php $your_email ='email@address';// <<=== update to your email address session_start(); $errors = ''; $name = ''; $visitor_email = ''; $visitor_website = ''; $user_message = ''; if(isset($_POST['submit'])) { $name = $_POST['name']; $visitor_email = $_POST['email']; $visitor_website = $_POST['website']; $user_message = $_POST['message']; ///------------Do Validations------------- if(empty($name)||empty($visitor_email)) { $errors .= "\n Name and Email are required fields. "; } if(IsInjected($visitor_email)) { $errors .= "\n Bad email value!"; } if(empty($_SESSION['6_letters_code'] ) || strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0) { //Note: the captcha code is compared case insensitively. //if you want case sensitive match, update the check above to // strcmp() $errors .= "\n The captcha code does not match!"; } if(empty($errors)) { //send the email $to = $your_email; $subject="New form submission"; $from = $your_email; $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''; $body = "A user $name submitted the contact form:\n". "Name: $name\n". "Email: $visitor_email \n". "Website Update?: $visitor_website \n". "Message: \n ". "$user_message\n". "IP: $ip\n"; $headers = "From: $from \r\n"; $headers .= "Reply-To: $visitor_email \r\n"; mail($to, $subject, $body,$headers); // header('Location: thank-you.html'); } } // Function to validate against any email injection attempts function IsInjected($str) { $injections = array('(\n+)', '(\r+)', '(\t+)', '(%0A+)', '(%0D+)', '(%08+)', '(%09+)' ); $inject = join('|', $injections); $inject = "/$inject/i"; if(preg_match($inject,$str)) { return true; } else { return false; } } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Contact Us</title> <!-- define some style elements--> <style> label,a, body { font-family : Arial, Helvetica, sans-serif; font-size : 12px; } .err { font-family : Verdana, Helvetica, sans-serif; font-size : 12px; color: red; } </style> <!-- a helper script for vaidating the form--> <script language="JavaScript" src="scripts/gen_validatorv31.js" type="text/javascript"></script> </head> <body> <?php if(!empty($errors)){ echo "<p class='err'>".nl2br($errors)."</p>"; } ?> <div id='contact_form_errorloc' class='err'></div> <form method="POST" name="contact_form" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>?page=contact"> <p> <label for='name'>Name: </label><br> <input type="text" name="name" value='<?php echo htmlentities($name) ?>'> </p> <p> <label for='email'>Email: </label><br> <input type="text" name="email" value='<?php echo htmlentities($visitor_email) ?>'> </p> <p> <label for='website'>Would you like to receive Website Update News? </label><br> <input type="radio" name="website" value='Yes <?php echo htmlentities($visitor_website) ?>' checked>YES <br> <input type="radio" name="website" value='No <?php echo htmlentities($visitor_website) ?>'>NO <br> </p> <p> <label for='message'>Message:</label> <br> <textarea name="message" rows=8 cols=30><?php echo htmlentities($user_message) ?></textarea> </p> <p> <img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br> <label for='message'>Enter the code above here :</label><br> <input id="6_letters_code" name="6_letters_code" type="text"><br> <small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small> </p> <input type="submit" value="Submit" name='submit'> </form> <script language="JavaScript"> var frmvalidator = new Validator("contact_form"); frmvalidator.EnableOnPageErrorDisplaySingleBox(); frmvalidator.EnableMsgsTogether(); frmvalidator.addValidation("name","req","Please provide your name"); frmvalidator.addValidation("email","req","Please provide your email"); frmvalidator.addValidation("email","email","Please enter a valid email address"); </script> <script language='JavaScript' type='text/javascript'> function refreshCaptcha() { var img = document.images['captchaimg']; img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000; } </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
bobd72 Posted July 27, 2010 Author Share Posted July 27, 2010 Thanks joel24. I couldn't find it at all - you're a whizz :-) problem solved. Quote Link to comment 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.