Jump to content

if statement help.


Recommended Posts

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

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.