ZeroShade Posted March 12, 2007 Share Posted March 12, 2007 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 More sharing options...
king Posted March 12, 2007 Share Posted March 12, 2007 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 More sharing options...
ZeroShade Posted March 12, 2007 Author Share Posted March 12, 2007 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 More sharing options...
king Posted March 12, 2007 Share Posted March 12, 2007 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 More sharing options...
ZeroShade Posted March 12, 2007 Author Share Posted March 12, 2007 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 More sharing options...
king Posted March 12, 2007 Share Posted March 12, 2007 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 More sharing options...
ZeroShade Posted March 12, 2007 Author Share Posted March 12, 2007 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? Link to comment Share on other sites More sharing options...
king Posted March 12, 2007 Share Posted March 12, 2007 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 More sharing options...
ZeroShade Posted March 12, 2007 Author Share Posted March 12, 2007 Thats the thing, I do not see the total shown after the submit is hit. Its like it refreshes the screen and the values in the text box are gone and the zero disappears. Any reason why its doing this for me? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 12, 2007 Share Posted March 12, 2007 Because it's not set up right. Post your code. Link to comment Share on other sites More sharing options...
ZeroShade Posted March 13, 2007 Author Share Posted March 13, 2007 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 More sharing options...
henryhenry Posted March 13, 2007 Share Posted March 13, 2007 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 More sharing options...
ZeroShade Posted March 13, 2007 Author Share Posted March 13, 2007 I'm trying to do it but I don't think I'm understanding... Link to comment Share on other sites More sharing options...
justsomeguy Posted March 13, 2007 Share Posted March 13, 2007 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 More sharing options...
ZeroShade Posted March 13, 2007 Author Share Posted March 13, 2007 ok... I'll play with this for a while. Thanks!! Link to comment Share on other sites More sharing options...
ZeroShade Posted March 13, 2007 Author Share Posted March 13, 2007 The reason the other variables are there is that I don't forget what they are worth later as I have to do more to it... however, its still not giving any variables when the submit is hit, its like it just refreshes? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 13, 2007 Share Posted March 13, 2007 That's not what I see when I run it, it works fine. Link to comment Share on other sites More sharing options...
ZeroShade Posted March 13, 2007 Author Share Posted March 13, 2007 Hmm... thats weird...Why would it do that then? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 13, 2007 Share Posted March 13, 2007 Either you aren't running the correct file, or PHP is not running. Look at the source code for the page in a web browser, and if you see any PHP code then PHP is not running. 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