ZeroShade Posted March 18, 2007 Share Posted March 18, 2007 Why won't my else if statement execute for my second radio button? Link to comment Share on other sites More sharing options...
Lulzim Posted March 19, 2007 Share Posted March 19, 2007 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 Link to comment Share on other sites More sharing options...
ZeroShade Posted March 19, 2007 Author Share Posted March 19, 2007 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 More sharing options...
Lulzim Posted March 19, 2007 Share Posted March 19, 2007 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 More sharing options...
ZeroShade Posted March 19, 2007 Author Share Posted March 19, 2007 Thanks!! Link to comment Share on other sites More sharing options...
ZeroShade Posted March 19, 2007 Author Share Posted March 19, 2007 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 More sharing options...
Lulzim Posted March 19, 2007 Share Posted March 19, 2007 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 More sharing options...
ZeroShade Posted March 19, 2007 Author Share Posted March 19, 2007 I did this and I get this error... Fatal error: Call to undefined function: ctype_digit() in /Users/100075255/Sites/PassengerTrain.php on line 21 Link to comment Share on other sites More sharing options...
Lulzim Posted March 19, 2007 Share Posted March 19, 2007 hmmm, you must have any problem with your server than, it works fine heretry is_int() or is_integer() instead of ctype_digit Link to comment Share on other sites More sharing options...
ZeroShade Posted March 19, 2007 Author Share Posted March 19, 2007 I don't get an error if use either of the two, but it doesn't skip to the else statement no matter what. So if its filled out correctly, it still doesn't work. Link to comment Share on other sites More sharing options...
ZeroShade Posted March 19, 2007 Author Share Posted March 19, 2007 Not only do I have this problem... but I can't figure out how to transfer the hours into minutes as well... if it prints 2.5 I want it to say 2 hours and 30 minutes, how would I go about doing this with the modulus operator? Link to comment Share on other sites More sharing options...
Mr_CHISOL Posted March 19, 2007 Share Posted March 19, 2007 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 More sharing options...
justsomeguy Posted March 19, 2007 Share Posted March 19, 2007 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now