Jump to content

AAAhhhhhh


ZeroShade

Recommended Posts

I can't get this to work at all!! So frustrated. I don't want the user to enter any values but numbers... ctype_digit only gives me and noone else errors. So i'm using is_integer... can somebody figure it out?

<!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" />								<link rel="stylesheet" href="styles.css" type="text/css" />				<title>Passenger Train</title>		</head>		<body>							 	<h1>Passenger Train</h1>								<h2>Calculate Time</h2>								<p />This service allows you to calculate the time it will take to reach your destination on our Passenger Train's. Please enter the distance you will be travelling, how many stops will occur, and if there is good or bad weather. You may use this service as many times as you wish. Have fun! 				<form action="PassengerTrain.php" method="post">						<?php								$speed = 50;								$weatherDecrease = 40;								$stop = 60 - ($numberOfStops * 5);								$miles = is_integer($_POST['miles']);								$numberOfStops = is_integer($_POST['numberOfStops']);																$weather = ($_POST['weather']);																$seconds = 60;																$minutes = $seconds / 60;																$hour = $minutes / 60;								if(isset($_POST['submit']))								{										if($miles == "" || $numberOfStops == "")												echo "<p />Please fill in your information appropriately. Values must be numeric.";										else										{												if($weather == '1')														$result = $stop * $miles / $speed;												else if($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='Calculate Time' name='submit' />";								echo $result;						?>				</form>								<img src="train.jpg" alt="" />		</body></html>

Link to comment
Share on other sites

Here is some sample code I got from PHP.net (I think) that will validate numbers. It is looking for any characters other than 0 - 9. I am not sure what happens with real numbers, I have not tried a decimal place. Give it a try. Hopefully it is a good start.

if(preg_match("/[^0-9\ ]+$/", $my_var)){// this is not a number, so do something}

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...