• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Problem with IF ELSE statement in PHP

rumpi

Junior Member
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:
For future reference, please use [code] tags next time. We also have special [php] tags just for PHP! 🙂
 
Back
Top