Jump to content

Why isn't this script waiting until my form is submitted before sending email?


adambedford

Recommended Posts

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.