prasadwalvekar Posted May 27, 2014 Share Posted May 27, 2014 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> Quote Link to comment Share on other sites More sharing options...
IanA Posted May 27, 2014 Share Posted May 27, 2014 (edited) 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 May 27, 2014 by IanA Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted May 27, 2014 Share Posted May 27, 2014 Side note: in case you're not aware, using PHP_SELF in the form's action tag opens the script to XSS attacks. More information can be found here: http://seancoates.com/blogs/xss-woes Quote Link to comment Share on other sites More sharing options...
prasadwalvekar Posted May 28, 2014 Author Share Posted May 28, 2014 Thank you all. Solved the problem using if(isset($subject)) { echo $subject; } if(isset($text)) { echo $text; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.