nschmutz Posted June 23, 2010 Share Posted June 23, 2010 I need help making this form operate correctly. First I need help making the form 'sticky'. I feel like I have the right code to make the form sticky but the number entered disappear once you hit the submit button. The other part is I could use help writing the correct code that displays the results based upon the drop-down menus. Right now no matter what is selected the results of all the functions are displayed and I am trying to just get the results from the drop-down selection results. Here is my code: <html> <head> <title>Assignment 2 Question 4</title> </head> <body> <form action="Assgn_2_Q4.php" method="POST"> <select name="entered"/> <option value="Square Root">Square Root</option> <option value="Square">Square</option> <option value="Cube">Cube</option> <h1>of</h1> <input type="text" name="entered" value="<?php $entered; ?>"/> <input type="submit" name="submit" value="is equal to" /> </form> <?php function square_root($n) { $result = round(sqrt($n), 3); return $result; }//end of function $entered = $_POST['entered']; if (isset($_POST['entered'])) { $entered = $_POST['entered']; $root = square_root($entered); echo "The square root of " . $entered . " is " . $root . "</br>"; }//end of isset function square($n) { $result = pow($n,2); return $result; }//end of function for square if (isset($_POST['entered'])) { $entered = $_POST['entered']; $square = square($entered); echo "The square of " . $entered . " is " . $square . "</br>"; }//end of isset for square function cube($n) { $result = pow($n,3); return $result; }//end of cube function if (isset($_POST['entered'])) { $entered = $_POST['entered']; $cube = cube($entered); echo "The square of " . $entered . " is " . $cube . "</br>"; }//end of isset for cube ?> </body> </html> Any help is deeply appreciated!! Quote Link to comment https://forums.phpfreaks.com/topic/205611-form-function/ Share on other sites More sharing options...
Ruzzas Posted June 23, 2010 Share Posted June 23, 2010 <input type="text" name="entered" value="<?php $entered; ?>"/> should be: <input type="text" name="entered" value="<?php echo $entered; ?>"/> Quote Link to comment https://forums.phpfreaks.com/topic/205611-form-function/#findComment-1075921 Share on other sites More sharing options...
nschmutz Posted June 23, 2010 Author Share Posted June 23, 2010 Thanks Ruzzas, I think my other problem is that I make my code more complicated than it needs to be but it is because I have a hard time understanding the logic that needs to be written. Do you know how I can present my code in the correct way and get the form to display the results only from the selection chosen? Quote Link to comment https://forums.phpfreaks.com/topic/205611-form-function/#findComment-1075933 Share on other sites More sharing options...
Ruzzas Posted June 23, 2010 Share Posted June 23, 2010 Well you could use: function square($n,$n2) { $result = pow($n,$n2); return $result; }//end of function for square for cube and square so you can just use square(10,2) for square or square(10,3) for cube. I'd change the function name though Quote Link to comment https://forums.phpfreaks.com/topic/205611-form-function/#findComment-1075935 Share on other sites More sharing options...
Ruzzas Posted June 23, 2010 Share Posted June 23, 2010 oh also you have two issets? just use one: if (isset($_POST['entered'])){ $entered = $_POST['entered']; $square = square($entered); echo "The square of $entered is $square</br>"; $entered = $_POST['entered']; $cube = cube($entered); echo "The square of $entered is $cube</br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/205611-form-function/#findComment-1075936 Share on other sites More sharing options...
nschmutz Posted June 23, 2010 Author Share Posted June 23, 2010 Thanks Ruzzas, so much. I can now easily read the code and it somewhat makes sense now. I am still having issues trying to get the results to line up with ONLY the selection from the drop-down menu. DO you know how I can correct this problem? Quote Link to comment https://forums.phpfreaks.com/topic/205611-form-function/#findComment-1075944 Share on other sites More sharing options...
Ruzzas Posted June 23, 2010 Share Posted June 23, 2010 Your </br>'s should be <br /> <select name="entered"/> <option value="Square Root">Square Root</option> <option value="Square">Square</option> <option value="Cube">Cube</option> should be: <select name="entered"> <option value="Square Root">Square Root</option> <option value="Square">Square</option> <option value="Cube">Cube</option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/205611-form-function/#findComment-1075947 Share on other sites More sharing options...
nschmutz Posted June 23, 2010 Author Share Posted June 23, 2010 Good catch Ruzzas on the </select>. I still have all the results being displayed after the number is submitted. Do you know what my problem is or how I should write the code to make it only enter the selected results. Do you think I need to make a foreach loop or for loop? Here is my updated code from your help: <html> <head> <title>Assignment 2 Question 4</title> </head> <body> <form action="Assgn_2_Q4.php" method="POST"> <select name="entered"/> <option value="Square Root">Square Root</option> <option value="Square">Square</option> <option value="Cube">Cube</option> </select> of <input type="text" name="entered" value="<?php echo $entered; ?>"/><!--making the form sticky--> <input type="submit" name="submit" value="is equal to" /> </form> <?php $entered = $_POST['entered'];//declaring $entered if (isset($_POST['entered'])) { $entered = $_POST['entered']; $root = square_root($entered); echo "The square root of " . $entered . " is " . $root . "<br />"; $entered = $_POST['entered']; $square = square($entered); echo "The square of " . $entered . " is " . $square . "<br />"; $entered = $_POST['entered']; $cube = cube($entered); echo "The square of " . $entered . " is " . $cube . "<br />"; }//end of isset function square_root($n) { $result = round(sqrt($n), 3); return $result; }//end of function function square($n) { $result = pow($n,2); return $result; }//end of function for square function cube($n) { $result = pow($n,3); return $result; }//end of cube function ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/205611-form-function/#findComment-1075959 Share on other sites More sharing options...
Ruzzas Posted June 23, 2010 Share Posted June 23, 2010 You can't make the name of the selection and input the same Here I've made it much cleaner with one function instead of three: <html> <head> <title>Assignment 2 Question 4</title> </head> <body> <form action="<?php echo $PHP_SELF;?>" method="POST"> <select name="entered"> <option value="Square Root">Square Root</option> <option value="Square">Square</option> <option value="Cube">Cube</option> </select> of <input type="text" name="number" value="<?php echo $entered; ?>"/><!--making the form sticky--> <input type="submit" name="submit" value="is equal to" /> </form> <?php if(isset($_POST['entered'])) { if($_POST["entered"] == "Square Root"){ $Calculate = calculate("square_root", $_POST['number'], 3); echo "The square root of " . $_POST['number'] . " is " . $Calculate . "<br />"; }elseif($_POST["entered"] == "Square"){ $Calculate = calculate("square", $_POST['number'], 2); echo "The square of " . $_POST['number'] . " is " . $Calculate . "<br />"; }elseif($_POST["entered"] == "Cube"){ $Calculate = calculate("cube", $_POST['number'], 3); echo "The cube of " . $_POST['number'] . " is " . $Calculate . "<br />"; } } function calculate($Function,$N1,$N2){ if($Function == "square_root"){ $Result = round(sqrt($N1), $N2); return $Result; }elseif($Function == "square"){ $Result = pow($N1, $N2); return $Result; }elseif($Function == "cube"){ $Result = pow($N1, $N2); return $Result; } } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/205611-form-function/#findComment-1075974 Share on other sites More sharing options...
nschmutz Posted June 23, 2010 Author Share Posted June 23, 2010 Thanks again Ruzzas. The other thing I was trying to prove in the form is that the number entered needs to be between 1 and 20. I was trying to display the error message. I am not sure where to place the error message or whether to create a loop of some kind. appreciate any help. Quote Link to comment https://forums.phpfreaks.com/topic/205611-form-function/#findComment-1075996 Share on other sites More sharing options...
Ruzzas Posted June 23, 2010 Share Posted June 23, 2010 Thanks again Ruzzas. The other thing I was trying to prove in the form is that the number entered needs to be between 1 and 20. I was trying to display the error message. I am not sure where to place the error message or whether to create a loop of some kind. appreciate any help. Thats simple all you need is one of these: if($_POST['number']>0.99&$_POST['number']<20.1){ Quote Link to comment https://forums.phpfreaks.com/topic/205611-form-function/#findComment-1076007 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.