jpopuk Posted August 4, 2009 Share Posted August 4, 2009 Hi, I have recently been using a simple enquiry form with some pretty basic PHP. The problem I am getting is when some one submits an enquiry it sends an email as it should but a couple of hours later it is sending a duplicate email. I have contacted my hosts and they say each email has a different ID so it must be within the script. Below is the code, if anyone can spot what could be causing this I would be most grateful. <?php $my_email = "name@domain.com"; $continue = "/"; $errors = array(); // Remove $_COOKIE elements from $_REQUEST. if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}} // Build message. function build_message($request_input){if(!isset($message_output)){$message_output ="";}if(!is_array($request_input)){$message_output = $request_input;}else{foreach($request_input as $key => $value){if(!empty($value)){if(!is_numeric($key)){$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;}else{$message_output .= build_message($value).", ";}}}}return rtrim($message_output,", ");} $message = build_message($_REQUEST); $message = stripslashes($message); $subject = "General Enquiry"; $subject = stripslashes($subject); $from_Name = ""; if(isset($_REQUEST['Name']) && !empty($_REQUEST['Name'])){$from_Name = stripslashes($_REQUEST['Name']);} $headers = "From: {$from_Name} <{$_REQUEST['Email']}>"; mail($my_email,$subject,$message,$headers); $message = "Thank you for your enquiry. We will be in touch with you as soon as possible. Regards, Website Name http://www.domain.com"; $subject = "General Enquiry"; $headers = "From: Website Name <noreply@domain.com>"; mail($_REQUEST['Email'],$subject,$message,$headers); ?> Many thanks, Paul Quote Link to comment Share on other sites More sharing options...
micah1701 Posted August 4, 2009 Share Posted August 4, 2009 is it doing it every time or just sometimes? it could be something as simple as the user going back to the page at a later time and the script being run again accidentally. Quote Link to comment Share on other sites More sharing options...
jpopuk Posted August 4, 2009 Author Share Posted August 4, 2009 It's doing it everytime. I have used it before on my server and it's worked fine. Just seems to be now, I contacted my host and they suggested seeing if I could get some help with the script but I thought it seemed ok. Thanks for the reply. Quote Link to comment Share on other sites More sharing options...
patrickmvi Posted August 4, 2009 Share Posted August 4, 2009 I couldn't see how that script is causing an email to be resent 4 hours later...unless what micah is saying is the case. If not, then I would think it would be some sort of issue with your hosting provider and they may be just telling you its your script because they just want to assume that nothing could possibly be wrong with their system. Quote Link to comment Share on other sites More sharing options...
jonsjava Posted August 4, 2009 Share Posted August 4, 2009 I'm working on cleaning up your code. is the $_REQUEST a $_GET or $_POST? Quote Link to comment Share on other sites More sharing options...
jpopuk Posted August 4, 2009 Author Share Posted August 4, 2009 I am not 100% sure to be honest. I would imagine it to be $_GEt. I am fairly new to PHP and learning a lot pretty quick. My host is Dreamhost which have been fantastic for as long as I've been using them. Cheers for the help guys, much appreciated! Quote Link to comment Share on other sites More sharing options...
jonsjava Posted August 4, 2009 Share Posted August 4, 2009 the form, does it say <form method="POST"> or <form method="GET">? Quote Link to comment Share on other sites More sharing options...
jpopuk Posted August 4, 2009 Author Share Posted August 4, 2009 It says: <form name="formcheck" action="/email.php" onsubmit="return formCheck(this);">[code] Cheers Quote Link to comment Share on other sites More sharing options...
jonsjava Posted August 4, 2009 Share Posted August 4, 2009 ah, so you're using ajax. It could still be either. I prefer to explicitly state in php which method to go against, so if you can look at the javascript function formCheck(), and look at the method, that would be helpful. Quote Link to comment Share on other sites More sharing options...
jpopuk Posted August 4, 2009 Author Share Posted August 4, 2009 This is the javascript that I use with this, doesn't have a formCheck() function. function formCheck(formobj){ // Enter name of mandatory fields var fieldRequired = Array("Name", "Email", "Enquiry"); // Enter field description to appear in the dialog box var fieldDescription = Array("Name", "Email", "Enquiry"); // dialog message var alertMsg = "Please complete the following fields:\n"; var l_Msg = alertMsg.length; for (var i = 0; i < fieldRequired.length; i++){ var obj = formobj.elements[fieldRequired[i]]; if (obj){ switch(obj.type){ case "select-one": if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){ alertMsg += " - " + fieldDescription[i] + "\n"; } break; case "select-multiple": if (obj.selectedIndex == -1){ alertMsg += " - " + fieldDescription[i] + "\n"; } break; case "text": case "textarea": if (obj.value == "" || obj.value == null){ alertMsg += " - " + fieldDescription[i] + "\n"; } break; default: } if (obj.type == undefined){ var blnchecked = false; for (var j = 0; j < obj.length; j++){ if (obj[j].checked){ blnchecked = true; } } if (!blnchecked){ alertMsg += " - " + fieldDescription[i] + "\n"; } } } } if (alertMsg.length == l_Msg){ return true; }else{ alert(alertMsg); return false; } }[code] Cheers for the help!! Quote Link to comment Share on other sites More sharing options...
jonsjava Posted August 4, 2009 Share Posted August 4, 2009 ok, rather than trying to guess, I've decided to try it this way: <?php $my_email = "name@domain.com"; $continue = "/"; $errors = array(); $method = "_POST"; // <-- If this fails, change to _GET // Build message. function build_message($request_input){ if(!isset($message_output)){ $message_output =""; } if(!is_array($request_input)){ $message_output = $request_input; } else{ foreach($request_input as $key => $value){ if(!empty($value)){ if(!is_numeric($key)){ $message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL; } else{ $message_output .= build_message($value).", "; } } } }return rtrim($message_output,", "); } $message = build_message($$method); $message = stripslashes($message); $subject = "General Enquiry"; $subject = stripslashes($subject); $from_Name = ""; if(isset($$method['Name']) && !empty($$method['Name'])){ $from_Name = stripslashes($$method['Name']); } $headers = "From: {$from_Name} <{$$method['Email']}>"; mail($my_email,$subject,$message,$headers); $message = "Thank you for your enquiry. We will be in touch with you as soon as possible. Regards, Website Name http://www.domain.com"; $subject = "General Enquiry"; $headers = "From: Website Name <noreply@domain.com>"; mail($$method['Email'],$subject,$message,$headers); ?> follow the instructions listed in the top. I didn't really change much in your code. It works fine. I just cleaned it up so it is standards-compliant. Quote Link to comment Share on other sites More sharing options...
jpopuk Posted August 4, 2009 Author Share Posted August 4, 2009 Many thanks, very much appreciated. 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.