Jump to content


Photo

Total newb needs help with if/else


  • Please log in to reply
5 replies to this topic

#1 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 13 June 2006 - 05:08 AM

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!

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 13 June 2006 - 05:13 AM


Try this
<?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?";
} 
?> 

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 13 June 2006 - 05:19 AM

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"


all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#4 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 13 June 2006 - 05:27 AM

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

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 13 June 2006 - 05:57 AM

Uou are referring to $_POST variables but the default method for forms is GET.

Specify form method as "POST"
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 13 June 2006 - 08:08 AM

[!--quoteo(post=383111:date=Jun 13 2006, 12:57 AM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Jun 13 2006, 12:57 AM) View Post[/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!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users