O'Reilly Forums: Chapter 4 Code Errors - O'Reilly Forums

Jump to content

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

Chapter 4 Code Errors sendmail.php script is not working

#1 User is offline   dannie_manji 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 01-February 12

Posted 13 March 2012 - 11:15 AM

i'm having problems with sendmail.php from chapter 4
here are the codes:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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>

<?php

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

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

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

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

if ((!empty($subject)) && (!empty($text))) {
$dbc = mysqli_connect('localhost', 'root', '', '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)){
$to = $row['email'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$msg = "Dear $first_name $last_name,\n$text";
mail($to, $subject, $msg, 'From:' . $from);
echo 'Email sent to: ' . $to . '<br />';
}

mysqli_close($dbc);
}

if ($output_form) {
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="subject">Subject of email:</label><br />
<input id="subject" name="subject" type="text" value="<?php echo $subject; ?>" size="30" /><br />
<label for="elvismail">Body of email:</label><br />
<textarea id="elvismail" name="elvismail" rows="8" cols="40"><?php echo $text; ?></textarea><br />
<input type="submit" name="submit" value="Submit" />
</form>

<?php
}
?>

</body>
</html>



the browser keeps telling me this Notice: Undefined variable: subject in C:\wamp\www\headfirst\ch04\final\makemeelvis\sendemail.php on line 68 Call Stack #TimeMemoryFunctionLocation 10.0023688128{main}( )..\sendemail.php:0 " size="30" />

what should i do?
0

#2 User is offline   djphinesse 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 16
  • Joined: 10-February 10

Posted 28 March 2012 - 06:03 AM

View Postlemuel, on 13 March 2012 - 11:50 AM, said:

The first time you run the script the form tries to use the variables $subject and $text, but no values have been assigned to them.

Change this:
<?php

if (isset($_POST['submit'])) {

to this:
<?php
$subject = '';
$text = '';

if (isset($_POST['submit'])) {



But how does your script know what to check for in the if statments if you initialize the variables with a null value instead of $_POST array?
0

#3 User is offline   djphinesse 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 16
  • Joined: 10-February 10

Posted 28 March 2012 - 07:09 AM

The problem is that because it's a test/dev server, xampp has the show PHP error enabled in the .ini file. So don't worry about the errors. When and if you upload to a production server, you won't even see those.

Here's my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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>
<?php
$from = 'sales@makemeelvis.com';
$subject = $_POST['subject'];
$text = $_POST['elvismail'];
$output_form = false;

if(isset($_POST['Submit'])){
  if(empty($subject) && empty($text)){
	  echo 'You forgot the email subject and body text. <br />';
	  $output_form = true;
  }
  if(empty($subject) && !empty($text)){
	  echo 'You forgot the email subject. <br />';
	  $output_form = true;
  }
  if(!empty($subject) && empty($text)){
	  echo 'You forgot the body text. <br />';
	  $output_form = true;
  }
  if (!empty($subject) && !empty($text)){
  // Connect to the database
  $dbc = mysqli_connect('localhost', 'elmer', '3ew7sOcI', '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)){
	  $to = $row['email'];
	  $first_name = $row['first_name'];
	  $last_name = $row['last_name'];
	  $msg = "Dear $first_name $last_name,\n$text";
	  mail($to, $subject, $msg, 'From:' . $from);
	  echo 'Email sent to: ' . $to . '<br />';
	} 
  mysqli_close($dbc);
  }
} else {
	$output_form = true;
  }
if ($output_form) {
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <label for="subject">Subject of email:</label><br />
    <input id="subject" name="subject" type="text" size="30" value="<?php echo $subject; ?>"/><br />
    <label for="elvismail">Body of email:</label><br />
    <textarea id="elvismail" name="elvismail" rows="8" cols="40"><?php echo $text; ?></textarea><br />
    <input type="submit" name="Submit" value="Submit" />
  </form>
<?php 
}
?>
</body>
</html>

0

#4 User is offline   Ev_Noi 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 22-July 13

Posted 22 July 2013 - 11:22 AM

Thank you so much, I got really stucked!
Thank yoy, my friend! :lol:
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