Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/205611-form-function/
Share on other sites

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?

 

Link to comment
https://forums.phpfreaks.com/topic/205611-form-function/#findComment-1075933
Share on other sites

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>";
}

Link to comment
https://forums.phpfreaks.com/topic/205611-form-function/#findComment-1075936
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/205611-form-function/#findComment-1075947
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/205611-form-function/#findComment-1075959
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/205611-form-function/#findComment-1075974
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/205611-form-function/#findComment-1075996
Share on other sites

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){

Link to comment
https://forums.phpfreaks.com/topic/205611-form-function/#findComment-1076007
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.