LiamProductions Posted July 1, 2007 Share Posted July 1, 2007 Hey. I need PHP help. Heres the code: <html> <head> <title>Hi User</title> </head> <body> <h1>Little Story maker</h1> <? print "<h3>$userName was walking down the street when someone asked him if he wanted to $hobbie he accepted the invite.</h3>"; if ($userName == Liam){ print "<h1>Your amazing!</h1>"; } // end if if ($userName == Andy){ print "<h1>The name andy is ok!</h1>"; } // end if if ($userName == Jack){ print "<h1>OMG! YOUR NAME IS JACK! I KNOW SOMEONE CALLED JACK</h1>"; } // end if ?> </body> </html> If your name is'nt any of the 3 how can i make it say "You've got a nice name" ? Thanks Quote Link to comment Share on other sites More sharing options...
trq Posted July 1, 2007 Share Posted July 1, 2007 <html> <head> <title>Hi User</title> </head> <body> <h1>Little Story maker</h1> <?php print "<h3>$userName was walking down the street when someone asked him if he wanted to $hobbie he accepted the invite.</h3>"; if ($userName == 'Liam'){ print "<h1>Your amazing!</h1>"; } // end if elseif ($userName == 'Andy'){ print "<h1>The name andy is ok!</h1>"; } // end if elseif ($userName == 'Jack'){ print "<h1>OMG! YOUR NAME IS JACK! I KNOW SOMEONE CALLED JACK</h1>"; } // end if else { print "You've got a nice name" ; } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 1, 2007 Share Posted July 1, 2007 <?php if($condition=='somevalue'){ //do something }elseif($condition=='someothervalue'){ //do seomthing else }else{ //do a default action } ?> So the bit you're missing is the elseif command. Alternatively, you can use switch statements: http://uk2.php.net/switch Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted July 1, 2007 Author Share Posted July 1, 2007 Thorpe, I tried your code. But even when i put Liam, Andy or Jack in, it still comes up with You've got a nice name... I want to stop that when using one of the 3 names but if you don't it will come up... Quote Link to comment Share on other sites More sharing options...
trq Posted July 1, 2007 Share Posted July 1, 2007 Where are you defining $userName ? Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted July 1, 2007 Author Share Posted July 1, 2007 I've redone it now... Name.php <html> <head> <title>My PHP Program!</title> </head> <body> <center> <?php if (empty($userName)){ print <<<HERE <form> Please enter your name: <br > <input type = "text" name = "userName"><br><br /> <input type = "submit" > </form> HERE; } else { print "<h3>Hi there, $userName!</h3>"; } //end if ($userName == Liam){ print "<h1>Your amazing!</h1>"; } // end if if ($userName == Andy){ print "<h1>The name andy is ok!</h1>"; } // end if if ($userName == Jack){ print "<h1>Jack thank you for using this</h1>"; } // end if if ($userName == Gav){ print "<h1>Nice Name. <br /></h1><br />$gavImg"; } // end if ?> </center> <br /> <br /> <br> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
no_one Posted July 1, 2007 Share Posted July 1, 2007 You have a few problems. $userName has to be set before you can test it for emptiness. Anyway, best to test the $_POST var first. <?php if( isset($_POST['userName'] ) { $userName = $_POST['userName']; /// other code } ?> You didn't set an action or method for your form, and you have 2 </form> <form method="post" action="name.php"> ... </form> The names being tested for (Liam, etc) should be in ' (single quote) or " (double quotes) (ex: 'Liam') unless they represent some define. Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted July 1, 2007 Author Share Posted July 1, 2007 The second code i work, works fine http://www.liamproductions.com/name.php I don't need a post method Quote Link to comment Share on other sites More sharing options...
trq Posted July 1, 2007 Share Posted July 1, 2007 So is your problem solved then? Also note, relying on the fact that register globals are switched on (ie; 'I don't need a post method') is a very bad idea. Register globals is being removed, and most servers have droped it already. 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.