The goal of the script is to validate an application that sends out emails to everyone listed in a database. The issue was that if you clicked submit with empty fields blank emails were sent. This was fixed by using the
empty()function and echo'ing back which part of the form was not filled out. In the book they say to use this method:
<?php
$header = 'From: elmer@makemeelvis.com';
$subject = $_POST['subject'];
$text = $_POST['elvismail'];
$output_form = false;
if(empty($subject) && empty($text))
{
echo 'Please enter a subject and message';
$output_form = true;
}
if(empty($subject) && (!empty($text))
{
echo 'Please enter a subject';
$output_form = true;
}
if((!empty($subject) && (empty($text))
{
echo 'Please enter a message';
$output_form = true;
}
if((!empty($subject)) && (!empty($text)))
{
$dbc = mysqli_connect('localhost', 'xxxxxx', 'xxxxxxx', 'elvis_store') or die('Error connecting to the database.');
$query = "SELECT * FROM email_list";
$result = mysqli_query($dbc, $query) or die('Error querying the database.');
while($row = mysqli_fetch_array($result))
{
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$msg = "Dear $first_name $last_name, \n $text";
$to = $row['email'];
mail($to, $subject, $msg, $header);
echo 'Email sent to: ' . $to . '</br>';
mysqli_close($dbc);
}
}
if($output_form)
{
?>
<form method="post" action="sendemail.php">
<label for="subject">Subject of email:</label><br />
<input id="subject" name="subject" type="text" size="30" /><br />
<label for="elvismail">Body of email:</label><br />
<textarea id="elvismail" name="elvismail" rows="8" cols="40"></textarea><br />
<input type="submit" name="Submit" value="Submit" />
</form>
<?php
}
?>
Now my method:
<?php
$header = 'From: elmer@makemeelvis.com';
$subject = $_POST['subject'];
$text = $_POST['elvismail'];
if((!empty($subject)) && (!empty($text)))
{
$dbc = mysqli_connect('localhost', 'xxxxxx', 'xxxxxx', 'elvis_store') or die('Error connecting to the database.');
$query = "SELECT * FROM email_list";
$result = mysqli_query($dbc, $query) or die('Error querying the database.');
while($row = mysqli_fetch_array($result))
{
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$msg = "Dear $first_name $last_name, \n $text";
$to = $row['email'];
mail($to, $subject, $msg, $header);
echo 'Email sent to: ' . $to . '</br>';
mysqli_close($dbc);
}
}
else
{
if(empty($subject))
{
echo 'Please enter a subject';
}
else if(empty($text))
{
echo 'Please enter a message';
}
else
{
echo 'Please enter a subject and message';
}
?>
<form method="post" action="sendemail.php">
<label for="subject">Subject of email:</label><br />
<input id="subject" name="subject" type="text" size="30" /><br />
<label for="elvismail">Body of email:</label><br />
<textarea id="elvismail" name="elvismail" rows="8" cols="40"></textarea><br />
<input type="submit" name="Submit" value="Submit" />
</form>
<?php
}
?>
My way is better because most of the time the form will be filled out and will be the first condition met. Secondly, and on the same note, it only has to check one(if the form is filled out) if statement opposed to five. Lastly, it has an extra unneeded variable and unneeded if statement to display the form.
^Am I right?
I understand that this question is about pointless but I am asking on terms of learning.
This post has been edited by D.Mulroy: 27 April 2011 - 01:34 PM

New Topic/Question
Reply




MultiQuote








|