O'Reilly Forums: Chapter 3 Pg.202 Validation And Pre Data Not Showing - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Chapter 3 Pg.202 Validation And Pre Data Not Showing

#1 User is offline   Dstock235 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 29-August 14

Posted 29 August 2014 - 06:43 AM

I'm running into a problem with using isset in an if statement to keep the data sticky and also allow for validation errors. For some reason when I put the data in, no matter if filled completely or not, it simply erases all the data within the input boxes and nothing more.

Here is my code:

Quote

<?php
if (isset($_POST['submit'])) {
$from = 'elmer@makemeelvis.com';
$subject = $_POST['subject'];
$text = $_POST['elvismail'];
$output_form = false;

if (empty($subject) && empty($text)) {
//both subject and text empty
echo 'You forgot both the subject and email text.<br />';
$output_form = true;
}

if (!empty($subject) && empty($text)) {
echo 'You forgot email text!!<br/>';
$output_form = true;
} //The text is forgotten

if (empty($subject) && (!empty($text))) {
echo 'You forgot the subject!<br/>';
$output_form = true;
} //The subject is forgotten

if (!empty($subject) && (!empty($text))) {
//Everything checks out and send email

$dbc = mysqli_connect('localhost', 'root', 'logman', 'elvis_store')
or die('Error connecting to MySQL server.');

$query = "SELECT * FROM email_list";
$result = mysqli_query($dbc, $query)
or die('Error querying 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, 'From:'. $from);

echo 'Email sent to:'.$to. '<br />';


}
mysqli_close($dbc);


}
}
else {
$output_form = true;
}

if ($output_form) {
?>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Make Me Elvis - Send Email</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<img src="blankface.jpg" width="161" height="350" alt="" style="float:right" />
<img name="elvislogo" src="elvislogo.gif" width="229" height="32" border="0" alt="Make Me Elvis" />
<p><strong>Private:</strong> For Elmer's use ONLY<br />
Write and send an email to mailing list members.</p>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<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>
</body>
</html>

<?php
}


?>


Maybe I am forgetting something? I don't know
0

#2 User is offline   Dstock235 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 29-August 14

Posted 01 September 2014 - 08:44 PM

View PostDstock235, on 29 August 2014 - 06:43 AM, said:

I'm running into a problem with using isset in an if statement to keep the data sticky and also allow for validation errors. For some reason when I put the data in, no matter if filled completely or not, it simply erases all the data within the input boxes and nothing more.

Here is my code:



Maybe I am forgetting something? I don't know


It is actually ch.4 :\
0

#3 User is offline   lemuel 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 577
  • Joined: 13-April 09

Posted 02 September 2014 - 10:13 AM

In your first line you check for the existence of the field named "submit". In your form the field has name "Submit", PHP is case sensitive.

Just one of a number of typos and errors in the book/code. You should bookmark
http://www.oreilly.c...n=9780596006303

and
http://www.oreilly.c...n=9780596006303
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users