Jump to content

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!
Link to comment
https://forums.phpfreaks.com/topic/11837-total-newb-needs-help-with-ifelse/
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]
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 ;)
[!--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!
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.