Jump to content

else if statement with radio button


ZeroShade

Recommended Posts

in order to help you show us some code or any error that you get, otherwise how do we know why it's not working :)
Now I feel dumb... I forgot the code :).
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">		<head>				<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />				<title>Passenger Train</title>				<!-- 60 x miles ÷ speed = T -->		</head>		<body>				<form action="PassengerTrain.php" method="post">						<?php								$speed = 50;								$weatherDecrease = 40;								$stop = 60 - ($numberOfStops * 5);								$miles = ($_POST['miles']);								$numberOfStops = $_POST['numberOfStops'];								if(isset($_POST['submit']))								{										if($miles == "" || $numberOfStops == "")												echo "<p />Please fill in your information appropriately.";										else										{												if(isset($_POST['weather']) == '1')														$result = $stop * $miles / $speed;												else if(isset($_POST['weather']) == '2')														$result = $stop * $miles / $weatherDecrease;										}								}								echo "<p />Miles:<input type='text' name='miles' value='' size='1' maxlength='3' />";								echo "<p />Number of stops:<input type='text' name='numberOfStops' value='' size='1' maxlength='3' />";								echo "<p />Good weather:<input type='radio' name='weather' value='1' checked='checked' />";								echo "<p />Bad weather:<input type='radio' name='weather' value='2' />";								echo "<input type='submit' value='OK' name='submit' />";								echo $result;						?>				</form>		</body></html>

Link to comment
Share on other sites

if(isset($_POST['weather']) == '1')$result = $stop * $miles / $speed;else if(isset($_POST['weather']) == '2')$result = $stop * $miles / $weatherDecrease;

in this case isset($_POST['weather']) returns true , you actualy are checking if (true=='1')so it should be

if($_POST['weather'] == '1')$result = $stop * $miles / $speed;else if($_POST['weather'] == '2')$result = $stop * $miles / $weatherDecrease;

also variable $numberOfStops is empty, use $_POST["numberOfStops"] instead

Link to comment
Share on other sites

Another question... I know that ctype_digit($var) will keep users from entering characters other then digits.. but how would I input that into my code... I tried, and I got it wrong?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">		<head>				<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />				<title>Passenger Train</title>		</head>		<body>				<form action="PassengerTrain.php" method="post">						<?php								$speed = 50;								$weatherDecrease = 40;								$stop = 60 - $numberOfStops * 5);								$miles = ($_POST['miles']);								$numberOfStops = $_POST['numberOfStops'];								if(isset($_POST['submit']))								{										if($miles == "" || $numberOfStops == "")												echo "<p />Please fill in your information appropriately.";										else										{												if($_POST['weather'] == '1')														$result = $stop * $miles / $speed;												else if($_POST['weather'] == '2')														$result = $stop * $miles / $weatherDecrease;										}								}								echo "<p />Miles:<input type='text' name='miles' value='' size='1' maxlength='3' />";								echo "<p />Number of stops:<input type='text' name='numberOfStops' value='' size='1' maxlength='3' />";								echo "<p />Good weather:<input type='radio' name='weather' value='1' checked='checked' />";								echo "<p />Bad weather:<input type='radio' name='weather' value='2' />";								echo "<input type='submit' value='OK' name='submit' />";								echo $result;						?>				</form>		</body></html>

Link to comment
Share on other sites

this is what i changed: if($miles == "" || $numberOfStops == "" || !ctype_digit($miles) || !ctype_digit($numberOfStops))

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">        <head>                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />                <title>Passenger Train</title>        </head>        <body>                <form action="PassengerTrain.php" method="post">                        <?php                        		$numberOfStops = $_POST["numberOfStops"];                                $speed = 50;                                $weatherDecrease = 40;                                $stop = (60 - $numberOfStops) * 5;                                $miles = ($_POST['miles']);                                $numberOfStops = $_POST['numberOfStops'];                                if(isset($_POST['submit']))                                {                                        if($miles == "" || $numberOfStops == "" || !ctype_digit($miles) || !ctype_digit($numberOfStops))                                                echo "<p />Please fill in your information appropriately.";                                        else                                        {                                                if($_POST['weather'] == '1')                                                        $result = $stop * $miles / $speed;                                                else if($_POST['weather'] == '2')                                                        $result = $stop * $miles / $weatherDecrease;                                        }                                }                                echo "<p />Miles:<input type='text' name='miles' value='' size='1' maxlength='3' />";                                echo "<p />Number of stops:<input type='text' name='numberOfStops' value='' size='1' maxlength='3' />";                                echo "<p />Good weather:<input type='radio' name='weather' value='1' checked='checked' />";                                echo "<p />Bad weather:<input type='radio' name='weather' value='2' />";                                echo "<input type='submit' value='OK' name='submit' />";                                echo $result;                        ?>                </form>        </body></html>

Link to comment
Share on other sites

Unless you don't have an OLD version of PHP (< 4.3) you shouldn't have a problem using ctype_digit() (If you have an old version DO UPGRADE).When it comes to is_int() it will return false if the variable is a string, so there's three ways to get this working:1. use ctype_digit() (!)2. use is_numeric(), note that this works on strings (as you get from POST/forms) and will return true if it's for ex. "2", BUT it will also return true if it's hex (0xFF), float (3.14) or exponential (+0123.45e6)3. "Cast" the value into int first or in the check using settype() Note that this will remove characters from the string, if there's any, and still return true...Hope that helped!Good Luck and Don't Panic!

Link to comment
Share on other sites

You can also use this to check for an integer:

if ($var != "" && intval($var) == $var) //$var is an integer

For hours and minutes:

$time = floatval($time_str);$hours = intval($time_str);$mins = round(($time - $hours) * 60);

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...