Jump to content

echoing a statment


ZeroShade

Recommended Posts

Why doesn't this want to show the value in the first text box when the button is submitted?

<!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" />				<!-- 60 x 25miles ÷ 15speed = T -->		</head>		<body>			<form action="PassengerTrain.php" method="POST">					<?php							$averageSpeed = 50;							$weatherDecrease = 10;							$stop = 0;							$miles = $_POST['miles'];							echo "<input type='text' name='miles' value='' size='1' maxlength='3' />";							echo "<input type='text' name='speed' value='' size='1' maxlength='3' />";							echo "<input type='submit' value='Submit' />";							echo $miles;					?>				</form>		</body></html>

Link to comment
Share on other sites

What do you mean it won't show the value in the first text box?Do you mean it won't show the value at all or it won't show the value inside the text box?

Link to comment
Share on other sites

What do you mean it won't show the value in the first text box?Do you mean it won't show the value at all or it won't show the value inside the text box?
What I'm trying to get it to do is the user inputs a value into the first textbox... submit the data... and then the last echo statement returns the value on the same page. The value shows in the textbox... its returning it on the same page.
Link to comment
Share on other sites

echo "<input type='text' name='miles' value='" . $miles . "' size='1' maxlength='3' />";echo "<input type='text' name='speed' value='' size='1' maxlength='3' />";echo "<input type='submit' value='Submit' />";

Well, that will return the value in the first textbox if I understood your question correctly. :)

Link to comment
Share on other sites

echo "<input type='text' name='miles' value='" . $miles . "' size='1' maxlength='3' />";echo "<input type='text' name='speed' value='' size='1' maxlength='3' />";echo "<input type='submit' value='Submit' />";

Well, that will return the value in the first textbox if I understood your question correctly. :)

I only want it to return after the submit button is hit... because i'm going to be adding equations such as 1 number times another + another and then the user hits submit and the total prints out... how can I do this?
Link to comment
Share on other sites

I only want it to return after the submit button is hit... because i'm going to be adding equations such as 1 number times another + another and then the user hits submit and the total prints out... how can I do this?
<!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" />				<!-- 60 x 25miles ÷ 15speed = T -->		</head>		<body>			<form action="PassengerTrain.php" method="POST">					<?php							$speed = $_POST['speed'];							$miles = $_POST['miles'];							$result = $miles + $speed;							echo "<input type='text' name='miles' value='' size='1' maxlength='3' />";							echo "<input type='text' name='speed' value='' size='1' maxlength='3' />";							echo "<input type='submit' value='Submit' />";							echo " ";							echo $result;					?>				</form>		</body></html>

That'll produce the sum of the first input + the second input. That was just an example because I don't know exactly what you're trying to do.

Link to comment
Share on other sites

Not exactly... basically I want it to work like a calculator and the submit is the equal button. How can I get it so that when the submit button is hit... the total is then shown?
The total is shown when you hit the submit button. I can't see the total before I hit the submit button; I can only see the number 0 before I hit the submit button.If you don't want the number 0...
echo "<input type='submit' value='Submit' name='submit' />";

$submit = $_POST['submit'];if ($submit == ""){echo "";}else{echo $result;

Link to comment
Share on other sites

Because it's not set up right. Post your code.
thats all of it. :)
<!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 25miles ÷ 15speed = T -->		</head>		<body>				<form action="PassengerTrain.php" method="POST">						<?php								$averageSpeed = 50;								$weatherDecrease = 10;								$hour = 60;								$stop = $hour - 5;								$miles = $_GET['miles'];								$speed = $_GET['speed'];								$result = $miles + $speed;								echo "<input type='text' name='miles' value='' size='1' maxlength='3' />";								echo "<input type='text' name='speed' value='' size='1' maxlength='3' />";								echo "<input type='submit' value='OK' />";								echo $result;						?>				</form>		</body></html>

Link to comment
Share on other sites

The way I learnt to do this was to use a hidden input <input type="hidden" name="submitted" value="TRUE" />Then outside your form you have a conditional a bit like...if (isset($_POST['submitted'])) { $miles = $_POST['miles']; $speed = $_POST['speed']; $result = $miles + $speed; echo $result; }maybe you don't need this conditional but it seems to work for me :) You definitely need to switch GET for POST if you use method="post" though...maybe that's where the error is.Also if you want to keep the values in the form fields after submitting then you can use a conditional inside the value=" " in the form itself:...value="<?php if(isset($_POST['miles'])) { echo $_POST['miles']; } ?>" ...That way, when you click submit, the value from the $_POST array is entered into the value=" " but when the page first loads it is just empty.Well I'm not sure if that helps but that's what I do :)

Link to comment
Share on other sites

I do almost the exact same thing, but I typically initialize my variables and skip the conditional in the value attribute. Setting your code up to do that would be something like this:

<?php$page_mode = (isset($_POST['page_mode']) ? $_POST['page_mode'] : "");$miles = "";$speed = "";$result = "";if ($page_mode == "submit"){  $miles = intval($_POST['miles']);  $speed = intval($_POST['speed']);  /*  You don't use any of these variables - why are they here?  $averageSpeed = 50;  $weatherDecrease = 10;  $hour = 60;  $stop = $hour - 5;  */  $result = $miles + $speed;}?><!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 25miles ÷ 15speed = T -->  </head>  <body><?phpif ($page_mode == "submit")  echo "<div style=\"text-align: center;\">The answer is {$result}</div>";?>	<form action="PassengerTrain.php" method="POST">	  <input type="hidden" name="page_mode" value="submit">	  <input type='text' name='miles' value='<?php echo $miles; ?>' size='1' maxlength='3' />	  <input type='text' name='speed' value='<?php echo $speed; ?>' size='1' maxlength='3' />	  <input type='submit' value='OK' />	</form>  </body></html>

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