...

View Full Version : PHP form sends emails twice for some reason...



sjwatsi
01-25-2008, 02:44 PM
I have a simple PHP form that I am practising with. It should send an email when I have filled out the fields and hit submit, but when I land on the page it will automatically send me a blank email.

I know this is probably something stupidly simple, but I would appeciate any incite into why this is.

I've included a simplified version of my code below. I hope someone can help.

Thanks

Swi.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<?php
$from = $_POST['from'];
$body = "Your message:\n {$_POST['bodyp1']}\n{$_POST['bodyp2']}";

mail( '###@###.co.uk', 'application assignment', $body, "From: {$from}" );
?>

<body>
<form id="form1" name="form1" method="post" action="register.php">
<label>from
<input type="text" name="from" />
</label>
<p>
<label>body
<input name="bodyp1" type="text" id="bodyp1" />
</label>
</p>
<p>
<label>part 2
<input name="bodyp2" type="text" id="bodyp2" />
</label>
</p>
<p>
<label>submit
<input type="submit" name="Submit" value="Register" />
</label>
</p>
</form>
</body>
</html>

angst
01-25-2008, 02:50 PM
yah, try:




$from = $_POST['from'];
if(IsSet($from)){
$body = "Your message:\n {$_POST['bodyp1']}\n{$_POST['bodyp2']}";
mail( '###@###.co.uk', 'application assignment', $body, "From: {$from}" );
}

sjwatsi
01-25-2008, 03:38 PM
Thanks for the speedy reply.

Can I just clarify? Does this if(IsSet($from)){} mean that if the from address has been set that the form can send an email? So if it isn't set you can't send an email?

Sorry I can't test it out right now - I wasn't expecting such a fast reply :) . But thanks for your you help! If I understood that right then that will be brilliant and sort of fixes another problem too :D .

Swi!

angst
01-25-2008, 03:40 PM
yup, you've got it.

sjwatsi
01-25-2008, 03:44 PM
Great. Thanks.

arnyinc
01-25-2008, 06:47 PM
Can I just clarify? Does this if(IsSet($from)){} mean that if the from address has been set that the form can send an email? So if it isn't set you can't send an email?

Just to clarify further... your php code was running every single time you loaded the page. So when you first loaded the page so you could type data into the fields it was already running the php script and sending an email (with those fields being blank).

Adding isset($from) makes sure that someone entered data into the form and clicked the submit button. Or more specifically it means that someone entered data in the "from" field and then clicked submit.

angola_g
01-25-2008, 07:56 PM
@arnyinc
Are you good with the PHP?I have a little Job for you please contact me on
lilanjaxson01@yahoo.com we need to sort out briefly.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum