p.utsav Posted September 4, 2009 Share Posted September 4, 2009 Ok, I am a beginner and I wrote this script. I dont know but it gives me a notice of "Undefined Index" for all the variables that I have declared here. Please look through it. <html> <title>A silly Poem</title> <body> <h2><u><i>Enter the following to read a story</h2></b></i><br><br><br> <form method="post" action="poem.php"> <h4>Enter your name: <input type="text" name="UserName" value=""><br> Enter your age: <input type="text" name="Age" value=""><br> Enter your sex: <input type="text" name="Sex" value="boy or girl"><br> Enter your favorite color: <input type="text" name="FavColor" value=""><br> Enter your favorite animal: <input type="text" name="FavAnimal" value=""><br> Enter your best friend's name: <input type="text" name="Friend" value=""><br> Enter the name of your favorite movie: <input type="text" name="FavMovie" value=""><br> Enter the name of your favorite actor: <input type="text" name="FavActor" value=""><br> <input type="submit" value="Submit"><br><br><br><br><center> <?php $UserName=$_POST['UserName']; $Age=$_POST['Age']; $Sex=$_POST['Sex']; $FavColor=$_POST['FavColor']; $FavAnimal=$_POST['FavAnimal']; $Friend=$_POST['Friend']; $FavMovie=$_POST['FavMovie']; $FavActor=$_POST['FavActor']; if ($Sex=="boy"){ print "Once upon a time there was a boy named $UserName. He was $Age years old. $UserName 's favorite color was $FavColor. He had a pet $FavAnimal. His best friend's name was $Friend. One day, they both went to watch $FavMovie together. To their surprise, they saw $FavActor. They were sooo happy, they took the autograph and returned home happily."; } else{ print "Once upon a time there was a girl named $UserName. She was $Age years old. $UserName 's favorite color was $FavColor. She had a pet $FavAnimal. Her best friend's name was $Friend. One day, they both went to watch $FavMovie together. To their surprise, they saw $FavActor. They were sooo happy, they took the autograph and returned home happily."; } ?> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/173073-unindentified-variable/ Share on other sites More sharing options...
FD_F Posted September 4, 2009 Share Posted September 4, 2009 you need use empty() or isset() example: if(!empty($_POST['Age'])) { $Age=$_POST['Age']; } Link to comment https://forums.phpfreaks.com/topic/173073-unindentified-variable/#findComment-912231 Share on other sites More sharing options...
thebadbad Posted September 4, 2009 Share Posted September 4, 2009 Please use or tags around the code you post. The undefined indexes are UserName, Age, Sex and so on in the $_POST array. They are undefined (not set) when the form hasn't been submitted. You can simple put the PHP code inside a conditional statement checking that $_POST is not returning false: <html> <title>A silly Poem</title> <body> <h2><u><i>Enter the following to read a story</h2></b></i><br><br><br> <form method="post" action="poem.php"> <h4>Enter your name: <input type="text" name="UserName" value=""><br> Enter your age: <input type="text" name="Age" value=""><br> Enter your sex: <input type="text" name="Sex" value="boy or girl"><br> Enter your favorite color: <input type="text" name="FavColor" value=""><br> Enter your favorite animal: <input type="text" name="FavAnimal" value=""><br> Enter your best friend's name: <input type="text" name="Friend" value=""><br> Enter the name of your favorite movie: <input type="text" name="FavMovie" value=""><br> Enter the name of your favorite actor: <input type="text" name="FavActor" value=""><br> <input type="submit" value="Submit"><br><br><br><br><center> <?php if ($_POST) { $UserName=$_POST['UserName']; $Age=$_POST['Age']; $Sex=$_POST['Sex']; $FavColor=$_POST['FavColor']; $FavAnimal=$_POST['FavAnimal']; $Friend=$_POST['Friend']; $FavMovie=$_POST['FavMovie']; $FavActor=$_POST['FavActor']; if ($Sex=="boy"){ print "Once upon a time there was a boy named $UserName. He was $Age years old. $UserName 's favorite color was $FavColor. He had a pet $FavAnimal. His best friend's name was $Friend. One day, they both went to watch $FavMovie together. To their surprise, they saw $FavActor. They were sooo happy, they took the autograph and returned home happily."; } else{ print "Once upon a time there was a girl named $UserName. She was $Age years old. $UserName 's favorite color was $FavColor. She had a pet $FavAnimal. Her best friend's name was $Friend. One day, they both went to watch $FavMovie together. To their surprise, they saw $FavActor. They were sooo happy, they took the autograph and returned home happily."; } } ?> </form> </body> </html> Or use !empty($_POST) as FD_F suggests. But you can't use isset($_POST), since the $_POST array always is set. Link to comment https://forums.phpfreaks.com/topic/173073-unindentified-variable/#findComment-912233 Share on other sites More sharing options...
bundyxc Posted September 4, 2009 Share Posted September 4, 2009 OP has extremely strange indentation style. I find it hard to read. Link to comment https://forums.phpfreaks.com/topic/173073-unindentified-variable/#findComment-912236 Share on other sites More sharing options...
thebadbad Posted September 4, 2009 Share Posted September 4, 2009 OP has extremely strange indentation style. I find it hard to read. It is. But his problem is easily solved though. Link to comment https://forums.phpfreaks.com/topic/173073-unindentified-variable/#findComment-912250 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.