Jump to content

Recommended Posts

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>

 

post-169037-0-09061400-1401170148_thumb.gif

Link to comment
https://forums.phpfreaks.com/topic/288798-basic-form-submission-code/
Share on other sites

The variable $subject is declared inside an if statement. You are then trying to access the variables outside this statement and if it hadn't been entered, the variables would not have been initialized. Try initializing the variables to null above the line:

 

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

Edited by IanA
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.