Problem with IF ELSE statement in PHP

rumpi

Junior Member
Sep 24, 2010
10
0
0
I am trying to make a list of ice cream and toppings using radio buttons. You can only have one flavor and one topping but certain combos are not allowed. I am trying to use and IF ..ELSE statement but it is not working. Is my syntax wrong for trying to include 2 variables for the && part of the if statement? Have I made some other error?

Here is the HTML page

1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Ice Cream Sundaes</title>
<meta http-equiv="Content-Type"
content="text/html; charset=utf-8" />

</head>






<body>

<form action="icecream.php" method="post">

<div align="left"><br>
<h4>Ice Cream Flavors</h4>
<input type="radio" name="flavor" value="Vanilla"> Vanilla<br>
<input type="radio" name="flavor" value="Chocolate"> Chocolate<br>
<input type="radio" name="flavor" value="Peanut Butter"> Peanut Butter<br>
<hr>
<h4>Toppings</h4>
<input type="radio" name="topping" value="Cherries"> Cherries<br>
<input type="radio" name="topping" value="Chocolate Sprinkles"> Chocolate Sprinkles<br>
<input type="radio" name="topping" value="Pineapple"> Pineapple<br>
<input type="radio" name="topping" value="Walnuts"> Walnuts<br>

<br><input type="submit" value="Get me some sweets!"><br>
<?php
?>

</body>
</html>


--------------------------------------------------------------------------
And the PHP response
--------------------------------------------------------------------


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="Content-Type"
content="text/html; charset=utf-8" />

</head>






<body>


<?php

$flavor = $_POST['flavor'];
$topping = $_POST['topping'];



if ($flavor == 'chocolate' && $topping == 'chocolate sprinkles')
{
print 'This would not be a good flavor combination, please try again.';
}

else {

print "You have chosen a $flavor Sundae with $topping, Good Eats!";

}





?>

</body>
</html>
 
Last edited:

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,816
75
For future reference, please use [code] tags next time. We also have special [php] tags just for PHP! :)