Jump to content


Photo

Basic "form Submission" Code


  • Please log in to reply
2 replies to this topic

#1 necker

necker

    New Member

  • Members
  • Pip
  • 2 posts

Posted 26 May 2014 - 09:37 PM

Hi all,

I browsed through various forums looking for a solution to the problem i am facing. i came upon a few similar problems others were facing but solution to their code does not seem to work with my code. i'll be grateful if someone could please point me in the right direction.

This is the notice that my browser shows me- Notice: Undefined variable: subject in C:\wamp\www\.....\sendemail4.php on line 69 Call Stack #TimeMemoryFunctionLocation 10.0010247784{main}( )..\sendemail4.php:0 ">

Also, please find attached a screenshot of what i am getting after running the php script in my localhost.

Here's the code from Head First php mysql book chapter 4 that i am trying to run-

<html>
<head>
<title> Make Me Elvis - Send Email </title>
</head>
<body>
<p><strong>Private:</strong> For Elmer's use only <br />
Write and send an email to mailing list numbers.</p>

<?php

//Detecting form submission using isset() function
//Making forms sticky

if (!empty($_POST['submit'])) {

$from = 'prasadwalvekar@yahoo.com';
$subject = $_POST['subject'];
$text = $_POST['elvismail'];
$output_form = false;

if( empty($subject) && empty($text) ) {
//Both $subject and $text are blank
echo 'You forgot the email subject and body text';
$output_form = true;

}

if( empty($subject) && !empty($text) ) {
//$subject is empty and $text is not empty
echo 'You forgot email subject.';
$output_form = true;
}

if( !empty($subject) && empty($text) ) {
//$subject is not empty and $text is empty
echo 'You forgot email body text.';
$output_form = true;
}
}

else {

$output_form = true; //if form's never been submitted, show it!
}

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']; ?>">
Subject of email: <input type="text" name="subject" value=" <?php echo $subject; ?> "><br />
Body of email: <textarea name = "elvismail" rows="8" cols="40"><?php echo $text; ?></textarea><br /> <br />
<input type = "submit" name = "submit" />
</form>

<?php

}
?>

</body>
</html>

Attached Thumbnails

  • oreilly.jpg


#2 lemuel

lemuel

    Super Veteran Member

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 577 posts

Posted 27 May 2014 - 06:09 AM

See comments regarding Page 204 here:
http://www.oreilly.c...n=9780596006303

#3 necker

necker

    New Member

  • Members
  • Pip
  • 2 posts

Posted 27 May 2014 - 10:04 PM

See comments regarding Page 204 here:
http://www.oreilly.c...n=9780596006303


Thanks a lot.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users