Jump to content

Archived

This topic is now archived and is closed to further replies.

simcoweb

Total newb needs help with if/else

Recommended Posts

Ok, I've already announced that i'm a newb. JUST learning PHP and going through literally dozens of tutorials and moving along. However, like I say to my proctologist... be gentle! :)

Here's the scoop. I've created a simple form in order to practice the if/else routines and experiment with the output. I already created a simulated order form and got that working without a hitch while swapping displayed messages depending upon how many of each item they ordered and also what their total number of items ordered. Worked great.

But! Now what I tried was to compare text input and 'if' it meets a certain name (in this case a game) then it would display one message and if they entered something else it would display a different message.

here's the code for the form:

<html>
<head>
</head>
<body>
<form name="shanetest" action="shanetest.php">
<font face="Verdana" size="2">Enter Your Favorite Game</font><br>
<input type="text" name="gamename" size="20"><p>
<font face="Verdana" size="2"><b>Your Gamer Tag</b></font><br>
<input type="text" name="gamertag" size="20"><p>
<input type="submit" name="submitinfo" value="Submit yo stuff" style="cursor:pointer">
</form>
</body>
</html>

And my code for the 'shanetest.php' file:

<?php
// First we create some variables
$gamertag = $_POST['gamertag'];
$gamename = $_POST['gamename'];
// Now we process the information
if ( $gamename == "halo" ) {
echo "You are the bomb, baby!<p>";
} else {
echo "you are a LOSER!<p>";
}
if ( $gamertag == "rockmetal" ) {
echo "That rocks, dude!<p>";
} else {
echo "Why would you play that lame game?";
}
?>

Now, all I get is the 'false' statements displayed. Even if you type in the proper entries of 'halo' and 'rockmetal' it won't display the 'if' true statements.

Once again, logically it should work. Code wise I must be missing something or trying to produce something that PHP needs additional info or a different operator.

I see lots of examples of this but none that showed the variables being populated by a form input and then being compared in an 'if' statement.

Can someone point me in the right direction? Thanks!

Share this post


Link to post
Share on other sites

Try this
[code]
<?php
// First we create some variables
$gamertag = $_POST['gamertag'];
$gamename = $_POST['gamename'];
// Now we process the information
if ( ! $gamename == "halo" ) {
echo "You are the bomb, baby!<p>";
} else {
echo "you are a LOSER!<p>";
}
if (!  $gamertag == "rockmetal" ) {
echo "That rocks, dude!<p>";
} else {
echo "Why would you play that lame game?";
}
?>
[/code]

Share this post


Link to post
Share on other sites
Sorry couldn't resist....

When asked by his patient why he used two fingers, the proctologist replied...

"I always like to get a second opinion"

Share this post


Link to post
Share on other sites
Hey..thanks for the quick reply.

Ok, tried that. I see you added some '!'characters in there. I also notice that their position in the statement is a little different but I don't think that makes a difference... does it?

Here's what happened when I made the changes. The 'true' statements printed but when entering the 'false' data (anything but what the variables hold true at which is 'halo' and 'rockmetal') then it still displays the 'true' statements. I type in anything in the two form fields and it displays the 'true' text. It's not parsing the 'false' statements.

So, adding those '!' now makes the true happen no matter what.

Just make sure that when your at the proctologists that while feeling the probe you don't also notice both his hands on your shoulders ;)

Share this post


Link to post
Share on other sites
Uou are referring to $_POST variables but the default method for forms is GET.

Specify form method as "POST"

Share this post


Link to post
Share on other sites
[!--quoteo(post=383111:date=Jun 13 2006, 12:57 AM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Jun 13 2006, 12:57 AM) [snapback]383111[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Uou are referring to $_POST variables but the default method for forms is GET.

Specify form method as "POST"
[/quote]


DOHHHH! Wow, can't believe I missed that. Ok, i'll post back with the results. Thanks!

Share this post


Link to post
Share on other sites

×

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.