moleyb Posted April 27, 2010 Share Posted April 27, 2010 Hi everyone, just getting into php and im still a complete noob. Iv got an php email form and its pissing me right off. Basically sending the same enquiry email twice (or more). Tried a few different forms I had with the same result. Just wondering if anyone can take a look at the following code, and see if its OK? Any help with this would be so much appreciated. Mike <?php #This is just a basic PHP e-mail form. #settings $youremail = "admin@nexus.com"; $thanks = "Thanks for your enquiry, we will get back to you shortly."; $emailnotvalid = "Please enter a valid e-mail address!"; $requiredfieldsempty = "Please fill in the required fields!"; #checks if email valid or not function checkemail($email) { if(ereg("^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.([a-zA-Z]{2,4})$", $email)) return true; else return false; } #email process is here. if (isset($_POST['submit'])) { #variables $name = $_REQUEST['name']; $email = $_REQUEST['email']; $url = $_REQUEST['url']; $subject = $_REQUEST['subject']; $message = $_REQUEST['message']; #checks if fields are empty or not. if(!empty($name) && !empty($email) && !empty($message)) { #checkemail function for email validating. if(checkemail($email)==true) { #final email. $result ="Name: $name \n\nEmail: $email \n\nUrl: $url \n\nSubject: $subject \n\nMessage: $message"; #sending email. mail( "$youremail", "Nexus Enquiry", $result, "From: $email" ); echo "<p id='success'><strong>$thanks</strong></p>"; #clearing fields after sending email. $name=null; $email=null; $url=null; $subject=null; $message=null; } else echo "<p id='failure'><strong>$emailnotvalid</strong></p>"; } else echo "<p id='failure'><strong>$requiredfieldsempty</strong></p>"; } ?> <form method="post" action="contact.php" id="contactform"> <fieldset> <label for="name">Name *</label> <input type="text" name="name" id="name" value="<?php echo $name; ?>" tabindex="1" /> <label for="email">Email *</label> <input type="text" name="email" id="email" value="<?php echo $email; ?>" tabindex="2" /> <label for="url">Company</label> <input type="text" name="url" id="url" value="<?php echo $url; ?>" tabindex="3" /> <label for="subject">Subject</label> <input type="text" name="subject" id="subject" value="<?php echo $subject; ?>" tabindex="4" /> <label for="message">Message *</label> <textarea name="message" id="message" tabindex="5" rows="5" cols="25"><?php echo $message; ?></textarea> <input type="submit" name="submit" value="Send" id="form-submit" class="submit-enquiry" tabindex="6" /> </fieldset> </form> Cheers guys, hope I can return the favor in the future! Quote Link to comment Share on other sites More sharing options...
coupe-r Posted April 27, 2010 Share Posted April 27, 2010 Everything looks fine to me. Comment out the mail() line and submit the form and see if an email is sent. Other than that, I'm at a loss. On a good note, it looks like your 508 compliant Quote Link to comment Share on other sites More sharing options...
moleyb Posted April 27, 2010 Author Share Posted April 27, 2010 Cheers for looking at that mate, what will commenting out the mail () part do? (and whats 508 compliant! sorry noob here) Quote Link to comment Share on other sites More sharing options...
coupe-r Posted April 27, 2010 Share Posted April 27, 2010 commenting out the mail function will still run the script, it just won't send out an email. I wanted to see if anything else was sending email besides that line. As for 508, this is a US government standard for people with disabilities (blind people). You have your label tags for all inputs. Disabled people use a program that reads off the <label> tags so they can hear what section they are on. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted April 27, 2010 Share Posted April 27, 2010 This might seem like a silly question, but is the e-mail that is sent to admin@nexus.com being forwarded to you twice? For example, my web host allows me to create "Forward-Only" e-mail addresses. Any e-mail sent to that address can then be forwarded to any number of addresses. Maybe the address you're forwarding to is listed twice. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted April 27, 2010 Share Posted April 27, 2010 Another quick question. Did you post the entire script? If not, it would be helpful to see all the code. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 27, 2010 Share Posted April 27, 2010 If that's all the code and the same thing happens with different code and you only get two emails sent each time, it is likely that your browser is requesting the page twice. This happens for different reasons for different browsers (at least FF and IE) and can also be caused by some URL rewriting on the server. What browser are you using, have your tried it with a different browser, and are you doing any URL rewriting on the server (even if it does not apply to the page you are trying)? A universal solution would be to use a session variable to prevent the form processing code from being executed more than once. Set a session variable at the point where you have successfully processed the form and skip the form processing code if that session variable is already set. Quote Link to comment Share on other sites More sharing options...
moleyb Posted April 27, 2010 Author Share Posted April 27, 2010 Thanks for your time with this guys, just been testing and it seems its FF causing the problem as it only sent one in chrome. Also commented out the mail() and it didnt send anything so I ruled that out. Sorry my php knowledge is seriously limited. How do you: set a session variable at the point where you have successfully processed the form and skip the form processing code if that session variable is already set. FYI This is the code im using: <?php #This is just a basic PHP e-mail form. #settings $youremail = "mikepostons@hotmail.com"; $thanks = "Thanks for your enquiry, we will get back to you shortly."; $emailnotvalid = "Please enter a valid e-mail address!"; $requiredfieldsempty = "Please fill in the required fields!"; #checks if email valid or not function checkemail($email) { if(ereg("^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.([a-zA-Z]{2,4})$", $email)) return true; else return false; } #email process is here. if (isset($_POST['submit'])) { #variables $name = $_REQUEST['name']; $email = $_REQUEST['email']; $url = $_REQUEST['url']; $subject = $_REQUEST['subject']; $message = $_REQUEST['message']; #checks if fields are empty or not. if(!empty($name) && !empty($email) && !empty($message)) { #checkemail function for email validating. if(checkemail($email)==true) { #final email. $result ="Name: $name \n\nEmail: $email \n\nUrl: $url \n\nSubject: $subject \n\nMessage: $message"; #sending email. mail( "$youremail", "Nexus Enquiry", $result, "From: $email" ); echo "<p id='success'><strong>$thanks</strong></p>"; #clearing fields after sending email. $name=null; $email=null; $url=null; $subject=null; $message=null; } else echo "<p id='failure'><strong>$emailnotvalid</strong></p>"; } else echo "<p id='failure'><strong>$requiredfieldsempty</strong></p>"; } ?> <form method="post" action="contact2.php" id="contactform"> <fieldset> <label for="name">Name *</label> <input type="text" name="name" id="name" value="<?php echo $name; ?>" tabindex="1" /> <label for="email">Email *</label> <input type="text" name="email" id="email" value="<?php echo $email; ?>" tabindex="2" /> <label for="url">Company</label> <input type="text" name="url" id="url" value="<?php echo $url; ?>" tabindex="3" /> <label for="subject">Subject</label> <input type="text" name="subject" id="subject" value="<?php echo $subject; ?>" tabindex="4" /> <label for="message">Message *</label> <textarea name="message" id="message" tabindex="5" rows="5" cols="25"><?php echo $message; ?></textarea> <input type="submit" name="submit" value="Send" id="form-submit" class="submit-enquiry" tabindex="6" /> </fieldset> </form> Cheers again for your time guys. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 27, 2010 Share Posted April 27, 2010 Basic one-shot logic to prevent reprocessing of submitted data - <?php session_start(); if(!isset($_SESSION['processed'])){ // put your form processing code here ... // at the point where your the processing code is compleately done, // set the session variable so that the processing code will be skipped until the session variable has been cleared - $_SESSION['processed'] = true; } ?> Quote Link to comment Share on other sites More sharing options...
moleyb Posted April 28, 2010 Author Share Posted April 28, 2010 Thanks @PFMaBiSmAd I havent had a chance to try it out yet. Ill get round to that tonight and let you guys know how that went. Quote Link to comment Share on other sites More sharing options...
moleyb Posted April 28, 2010 Author Share Posted April 28, 2010 Hi there, im guessing I done this completely wrong, but im getting an error appearing on the page at the start of the form: Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /websites/123reg/LinuxPackage21/ne/xu/s_/nexus-defence.com/public_html/contact3.php:6) in /websites/123reg/LinuxPackage21/ne/xu/s_/nexus-defence.com/public_html/contact3.php on line 49 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /websites/123reg/LinuxPackage21/ne/xu/s_/nexus-defence.com/public_html/contact3.php:6) in /websites/123reg/LinuxPackage21/ne/xu/s_/nexus-defence.com/public_html/contact3.php on line 49 If anyone has a moment to look over it and see where iv cocked it up , id really appreciate it. Thanks <?php session_start(); if(!isset($_SESSION['processed'])){ // put your form processing code here ... #This is just a basic PHP e-mail form. #settings $youremail = "mikepostons@hotmail.com"; $thanks = "Thanks for your enquiry, we will get back to you shortly."; $emailnotvalid = "Please enter a valid e-mail address!"; $requiredfieldsempty = "Please fill in the required fields!"; #checks if email valid or not function checkemail($email) { if(ereg("^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.([a-zA-Z]{2,4})$", $email)) return true; else return false; } #email process is here. if (isset($_POST['submit'])) { #variables $name = $_REQUEST['name']; $email = $_REQUEST['email']; $url = $_REQUEST['url']; $subject = $_REQUEST['subject']; $message = $_REQUEST['message']; #checks if fields are empty or not. if(!empty($name) && !empty($email) && !empty($message)) { #checkemail function for email validating. if(checkemail($email)==true) { #final email. $result ="Name: $name \n\nEmail: $email \n\nUrl: $url \n\nSubject: $subject \n\nMessage: $message"; #sending email. mail( "$youremail", "Nexus Enquiry", $result, "From: $email" ); echo "<p id='success'><strong>$thanks</strong></p>"; #clearing fields after sending email. $name=null; $email=null; $url=null; $subject=null; $message=null; } else echo "<p id='failure'><strong>$emailnotvalid</strong></p>"; } else echo "<p id='failure'><strong>$requiredfieldsempty</strong></p>"; } // at the point where your the processing code is compleately done, // set the session variable so that the processing code will be skipped until the session variable has been cleared - $_SESSION['processed'] = true; } ?> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 28, 2010 Share Posted April 28, 2010 output started at ...../contact3.php:6 (line 6) Something at or up to line 6 in the file is being output to the browser and is preventing the session_start() from working on line 49. The session_start() must come before anything other than a header() is output to the browser. Quote Link to comment Share on other sites More sharing options...
moleyb Posted April 28, 2010 Author Share Posted April 28, 2010 Cheers for taking a look. Is that line 6 of the whole web page or line 6 of the php form code? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 29, 2010 Share Posted April 29, 2010 The file in question is shown in the error message and also in the quoted portion of the error in my reply. 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.