adambedford Posted February 10, 2010 Share Posted February 10, 2010 I've written this script that goes before the <html> tag of the page so that my form can be processed on the same page. Further down the page, in the HTML I've got a form with the action"$PHP_SELF". $name = $_POST["Name"]; $email = $_POST["Email"]; $message = $_POST["Message"]; if(!isset($name) || !isset($email) || !isset($message)) { $to = "[email protected]"; $subject = "Someone sent you a message from your website!"; $message = "Name:".$name."\n\n"."Email:".$email."\n\n"."Message:".$message; $headers = "From:".$email."\r\n"; //$emailtosend = "$to.','. $subject.','. $message.','. null.','. -f$email;" $emailtosend = mail($to, $subject, $message, null, $headers); if (isset($emailtosend)) { $confmessage = "Your message has now been sent. Thank you."; } } I also echo a message to say that the form has been submitted successfully using this: <?php if (isset($emailtosend)) { echo $confmessage; } ?> I wrote this myself so I'm not exactly sure it's the best way to go about the situation. The problem is that when the page loads, the email gets sent immediately and the success message shows. Obviously this isn't right as the user doesnt even get the chance to fill in the form and hit Submit. Any idea what's going wrong? Link to comment https://forums.phpfreaks.com/topic/191666-why-isnt-this-script-waiting-until-my-form-is-submitted-before-sending-email/ Share on other sites More sharing options...
sader Posted February 10, 2010 Share Posted February 10, 2010 try change these: if(!isset($name) || !isset($email) || !isset($message)) => if($name && $email && $message) if (isset($emailtosend)) => if($emailtosend) Link to comment https://forums.phpfreaks.com/topic/191666-why-isnt-this-script-waiting-until-my-form-is-submitted-before-sending-email/#findComment-1010300 Share on other sites More sharing options...
adambedford Posted February 10, 2010 Author Share Posted February 10, 2010 worked like a charm. thank you! Link to comment https://forums.phpfreaks.com/topic/191666-why-isnt-this-script-waiting-until-my-form-is-submitted-before-sending-email/#findComment-1010307 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.