Jump to content

Email form sends twice


moleyb

Recommended Posts

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!

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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;
}
?>

Link to comment
Share on other sites

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;
				}
				?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.