View Full Version : "Empty" function when sending mail


Turquoise
07-19-2006, 05:51 PM
Hi there, this is coming from a definate beginner at PHP programming.

I've been trying to make a mail form for the visitors to my website and decided to stop people from sending mails with empty fields by using the empty function as part of an if..else statement.

The problem is the server sees that the input boxes are empty as soon as the page is loaded and so the message "You didn't fill in all of the fields. Please try again." is displayed just by visiting the page the feedback form is on.

Here is the page:
http://www.nonplussedgraphics.com/feedback.php

Is there a relatively simple way to code this, keeping the feedback form and the PHP in the same page, so that the echo messages are only triggered after the submit button is pressed?

Oh, this is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Nonplussed - Feedback</title>
<?php include('top.php'); ?>
</head>
<body>
<div id="bucket"></div>
<div id="header"></div>
<div id="left"><?php include('left.php'); ?></div>
<div id="right">
<h1>Feedback</h1>
<p>Like the site? Think it could be improved? Why not send me some feedback?</p>
<form method="post" action="feedback.php">
<p>E-mail: <br /><input type="text" name="email" /><br />
Name: <br /><input type="text" name="name" /><br />
Comments: <br /><textarea rows="6" cols="15" name="comments"></textarea><br /></p>
<p><input type="submit" value="Send me your comments!" /></p>
</form>
<?php
$email=$_REQUEST['email'];
$name=$_REQUEST['name'];
$comments=$_REQUEST['comments'];
if (empty($email))
{
echo "You didn't fill in all of the fields. Please try again.";
}

elseif (empty($name))
{
echo "You didn't fill in all of the fields. Please try again.";
}

elseif (empty($comments))
{
echo "You didn't fill in all of the fields. Please try again.";
}

else
{
mail("mail@nonplussedgraphics.com","Nonplussed Feedback",$comments,"From:$email\n$name");
echo "Your mail has been sent successfully. Thanks for the feedback!";
}
?>
</div>
</body>
</html>

I hope my explanation isn't too confusing!

Douglas
07-19-2006, 08:48 PM
Try this:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Nonplussed - Feedback</title>
<?php include('top.php'); ?>
</head>
<body>
<div id="bucket"></div>
<div id="header"></div>
<div id="left"><?php include('left.php'); ?></div>
<div id="right">
<h1>Feedback</h1>
<p>Like the site? Think it could be improved? Why not send me some feedback?</p>
<form method="post" action="feedback.php">
<input type="hidden" name="process" value="yes" />
<p>E-mail: <br /><input type="text" name="email" /><br />
Name: <br /><input type="text" name="name" /><br />
Comments: <br /><textarea rows="6" cols="15" name="comments"></textarea><br /></p>
<p><input type="submit" value="Send me your comments!" /></p>
</form>
<?php
if (isset($_POST['process'])) {
$email=$_REQUEST['email'];
$name=$_REQUEST['name'];
$comments=$_REQUEST['comments'];
if (empty($email))
{
echo "You didn't fill in all of the fields. Please try again.";
}

elseif (empty($name))
{
echo "You didn't fill in all of the fields. Please try again.";
}

elseif (empty($comments))
{
echo "You didn't fill in all of the fields. Please try again.";
}

else
{
mail("mail@nonplussedgraphics.com","Nonplussed Feedback",$comments,"From:$email\n$name");
echo "Your mail has been sent successfully. Thanks for the feedback!";
}
}
?>
</div>
</body>
</html>

Turquoise
07-19-2006, 08:51 PM
It worked! And it was so simple even I could understand what it was doing.
Thanks ever so much!

Douglas
07-19-2006, 11:23 PM
Haha no problem, good luck with whatever you're working on :D

2gq4u
07-20-2006, 10:08 PM
Thanks guys this post really helped me out