simple mail function questioni

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dru_nasty
Forum Commoner
Posts: 81
Joined: Sat Sep 10, 2005 10:26 am

simple mail function questioni

Post by dru_nasty »

Code: Select all

<?php
$to = "my email"; 
$subject = "Testing PHP on Server";
$message = "Mail is working"; 

if(submit){

if(mail($to,$subject,$message)){
	echo "mail sent";
	}else{
	echo "mail NOT sent";
	}
}
?> 

<html> 
<head> 
<title></title> 
</head> 
<body> 

<form method="post" action="<? echo($PHP_SELF) ?>"> 
<input type="submit" value="Submit"> 
</form>

</body> 
</html>
My question is why when i hit this page, the "mail sent" echo is already showing before clicking the submit button?
How can i fix this so no echo shows until after the mail function is ran?
Thanks!!
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Change if(submit) to
if (isset($_POST['submit']))
dru_nasty
Forum Commoner
Posts: 81
Joined: Sat Sep 10, 2005 10:26 am

Post by dru_nasty »

Code: Select all

<html> 
<head> 
<title></title> 
</head> 
<body> 
<?php 
if ($_POST['submit']=='Submit') 
{ 
$to = "address here"; 
$subject = "Testing PHP Server"; 
$message = "Mail is working"; 
$header = 'From: address here' . "\r\n"; 
if(mail($to,$subject,$message,$header)) 
{ 
echo "mail sent"; 
} 
else 
{ 
echo "mail NOT sent"; 
} 
} 
?> 

<form method="post" action="<? echo($PHP_SELF) ?>"> 
<input type="submit" value="Submit" name="submit"> 
</form> 

</body> 
</html>
Thx for the reply, I just actually figured it out and got it working
Post Reply