Jimbolgs
11-21-2011, 07:59 PM
Can anybody help with this please — it's a form validation exercise from a book I'm following.
If you remove lines 4 and 25 the script works, except it returns the error message I've built in on line 11 when the form has never been filled in.
Lines 4 and 25 are what the book tells me to add to stop this error message from happening when the page first loads, but then the script doesn't work.
Thanks in advance for any help.
<p class="body">
<?php
if (isset($_POST['submit'])) { //This is the start of the problem line
$from = 'me@gmail.com'; //What mail recipient will see as mail sender
$subject = $_POST['subject']; //Field from form on send_email.html
$message = $_POST['message']; //Field from form on send_email.html
$output = false; //Flag for whether to re-output form set to false
if (empty($subject) && empty($message)) {
echo '<b>Error:</b> Please enter a subject and message';
$output = true;
}
if (empty($subject) && (!empty($message))) {
echo '<b>Error:</b> Please enter a subject';
$output = true;
}
if ((!empty($subject)) && empty($message)) {
echo '<b>Error:</b> Please enter a message';
$output = true;
}
}//This closes the problem line
else {
$output = true;
}
if ((!empty($subject)) && (!empty($message))) {
$dbc = mysqli_connect('localhost', 'xxx', 'xxx', 'xxx') //Open database connection
or die('Error connecting to MySQL database'); //Or die trying
$query = "SELECT * FROM elvis_list"; //Query - select everything from the elvis_list table in the database above
$result = mysqli_query($dbc, $query) //'Result' is a new variable which is the result of the dbc and query above
or die('Error querying MySQL database'); //Or die trying
echo '<b>Email sent to: </b><br /><br />'; //
while ($row = mysqli_fetch_array($result)) { //Begin send email loop / where '$row' is a new variable / containing a row of info, or 'array' from '$result' variable
$to = $row['email']; //'$to' is a new variable using the 'email' column from '$row' above
$first_name = $row['first_name']; //'$first_name' is a new variable using the 'first_name' column from '$row' above
$last_name = $row['last_name']; //'$last_name' is a new variable using the 'last_name' column from '$row' above
$msg = "Dear $first_name $last_name, \n $message"; //'$msg' is a new variable containing the string "Dear 'see variables above'"
mail($to, $subject, $msg, 'from: ' . $from); //'mail' is a PHP command and the rest of this line is the variables with which to construct the message
echo $to . '<br />'; //Echo string containing 'to' varable back to page
}
mysqli_close($dbc); //Close database connection
}
if ($output) { //Output form if true
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p class="body"><label for="subject">Subject:</label>
<input type="text" id="subject" name="subject" value="<?php echo $subject ?>" /><br /></p>
<br />
<p class="body"><label for="message">Message:</label>
<textarea name="message" cols="50" rows="5" id="message"><?php echo $message ?></textarea>
<br /></p>
<br />
<p class="submit"><input type="submit" id="submit" value="Send message" /></p>
</form>
<?php
} //Jump back into PHP to close if statement
?>
</p>
If you remove lines 4 and 25 the script works, except it returns the error message I've built in on line 11 when the form has never been filled in.
Lines 4 and 25 are what the book tells me to add to stop this error message from happening when the page first loads, but then the script doesn't work.
Thanks in advance for any help.
<p class="body">
<?php
if (isset($_POST['submit'])) { //This is the start of the problem line
$from = 'me@gmail.com'; //What mail recipient will see as mail sender
$subject = $_POST['subject']; //Field from form on send_email.html
$message = $_POST['message']; //Field from form on send_email.html
$output = false; //Flag for whether to re-output form set to false
if (empty($subject) && empty($message)) {
echo '<b>Error:</b> Please enter a subject and message';
$output = true;
}
if (empty($subject) && (!empty($message))) {
echo '<b>Error:</b> Please enter a subject';
$output = true;
}
if ((!empty($subject)) && empty($message)) {
echo '<b>Error:</b> Please enter a message';
$output = true;
}
}//This closes the problem line
else {
$output = true;
}
if ((!empty($subject)) && (!empty($message))) {
$dbc = mysqli_connect('localhost', 'xxx', 'xxx', 'xxx') //Open database connection
or die('Error connecting to MySQL database'); //Or die trying
$query = "SELECT * FROM elvis_list"; //Query - select everything from the elvis_list table in the database above
$result = mysqli_query($dbc, $query) //'Result' is a new variable which is the result of the dbc and query above
or die('Error querying MySQL database'); //Or die trying
echo '<b>Email sent to: </b><br /><br />'; //
while ($row = mysqli_fetch_array($result)) { //Begin send email loop / where '$row' is a new variable / containing a row of info, or 'array' from '$result' variable
$to = $row['email']; //'$to' is a new variable using the 'email' column from '$row' above
$first_name = $row['first_name']; //'$first_name' is a new variable using the 'first_name' column from '$row' above
$last_name = $row['last_name']; //'$last_name' is a new variable using the 'last_name' column from '$row' above
$msg = "Dear $first_name $last_name, \n $message"; //'$msg' is a new variable containing the string "Dear 'see variables above'"
mail($to, $subject, $msg, 'from: ' . $from); //'mail' is a PHP command and the rest of this line is the variables with which to construct the message
echo $to . '<br />'; //Echo string containing 'to' varable back to page
}
mysqli_close($dbc); //Close database connection
}
if ($output) { //Output form if true
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p class="body"><label for="subject">Subject:</label>
<input type="text" id="subject" name="subject" value="<?php echo $subject ?>" /><br /></p>
<br />
<p class="body"><label for="message">Message:</label>
<textarea name="message" cols="50" rows="5" id="message"><?php echo $message ?></textarea>
<br /></p>
<br />
<p class="submit"><input type="submit" id="submit" value="Send message" /></p>
</form>
<?php
} //Jump back into PHP to close if statement
?>
</p>