vipsa Posted November 6, 2013 Share Posted November 6, 2013 (edited) Hi I am a newbie. I get an error undefined variable subject and it is the line in the form where subject is but how can the variable be undefined if I define it in the beginning? Why then is the other variable $text also then not undefined. I also get allot of css code in the form <?php if(!isset($_POST['submit'])) { $from = 'corne@viprojects.net'; $subject = $_POST['subject']; $text = $_POST['emailbody']; $output_form = false; if(empty($subject) && empty($text)) { echo 'You forgot the subject and text'; } $output_form = true; if(empty($subject) && !empty($text)) { echo 'You forgot to enter the subject'; } $output_form = true; if(!empty($subject) && empty($text)) { echo 'You forgot to enter the text'; } } else { $output_form = true; } if($output_form) { ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <label for="subject">Subject</label><br> <input type="text" id="subject" name="subject" value="<?php echo $subject; ?>"><br> <label for="emailbody">Email body</label><br> <textarea id="emailbody" name="emailbody" rows="5" cols="20" ><?php echo $text; ?></textarea><br> <input type="submit" name="submit" value="submit"> </form>' <?php } ?> Edited November 6, 2013 by vipsa Quote Link to comment https://forums.phpfreaks.com/topic/283642-undefined-variable-but-how/ Share on other sites More sharing options...
requinix Posted November 6, 2013 Share Posted November 6, 2013 $subject won't be defined if the form has been submitted. On that note, you have your logic backwards in that first if block. Quote Link to comment https://forums.phpfreaks.com/topic/283642-undefined-variable-but-how/#findComment-1457165 Share on other sites More sharing options...
vipsa Posted November 7, 2013 Author Share Posted November 7, 2013 OK but before I submit that form it gives me this error and I got this code from a book so please tell me how the logic is backwards in the first block Quote Link to comment https://forums.phpfreaks.com/topic/283642-undefined-variable-but-how/#findComment-1457306 Share on other sites More sharing options...
cyberRobot Posted November 7, 2013 Share Posted November 7, 2013 ...please tell me how the logic is backwards in the first block The following tests if the POST variable for submit isn't set. if(!isset($_POST['submit'])) { That part of your code should only run if the submit button was clicked. Quote Link to comment https://forums.phpfreaks.com/topic/283642-undefined-variable-but-how/#findComment-1457308 Share on other sites More sharing options...
cyberRobot Posted November 7, 2013 Share Posted November 7, 2013 Side note: You should take a look at the following article which talks about the problems with using PHP_SELF as the action attribute for a form: http://seancoates.com/blogs/xss-woes Quote Link to comment https://forums.phpfreaks.com/topic/283642-undefined-variable-but-how/#findComment-1457310 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.