0

I have an html form to get a number from the user for a calculation. The first number is given. The second is entered by the user. The result is posted to the same page. BUT the resulting answer is now the first number and again the second number is entered by the user. The first code works fine for the first calculation. But the next ones still use the given number, not the result. I believe I should use an if/else to test if the submit was entered. But the first answer isn't being passed on the next go round with $first=$total (I get undefined variable total).

Without the else statement:

 <head> 
 <title>Calculate</title> 
 </head> 
 <body> 

 <?php 
 $first = 2;

 if(isset($_POST['Calculate'])) 
     {
      $second = (!empty($_POST['second']) ? $_POST['second'] : null);
      $total = $first+$second; 
      print "<h2>Results</h2>"; 
      print "The total of the two numbers: $first + $second = $total <p>"; 
      $first=$total;
     }

 ?> 



 <h2>Calculate</h2> 
 <p><?php echo "First Number: $first"; ?></p> 
 <br>
 <form action = "index.php" method = "POST"> 
  Second Number: <input type = "text" name = "second"><br>
 <input type = "submit" name = "Calculate"/> 
 </form> 
 </body> 
 </html> 

With the else statement:

 <head> 
 <title>Calculate</title> 
 </head> 
 <body> 

 <?php 


 if(isset($_POST['Calculate'])) 
      {
      $first=$total;
      $second = (!empty($_POST['second']) ? $_POST['second'] : null);
      $total = $first+$second; 
      print "<h2>Results</h2>"; 
      print "The total of the two numbers: $first + $second = $total <p>"; 
      }
 else {
       $first = 2;
       $second = (!empty($_POST['second']) ? $_POST['second'] : null);
       $total = $first+$second;
       }
 ?> 



 <h2>Calculate</h2> 
 <p><?php echo "First Number: $first"; ?></p> 
 <br>
 <form action = "index.php" method = "POST"> 
  Second Number: <input type = "text" name = "second"><br>
 <input type = "submit" name = "Calculate"/> 
 </form> 
 </body> 
 </html> 

I changed the form to

<form action = "index.php" method = "POST"> 
 <input type="hidden" name="first" value="$first" />

  Second Number: <input type = "text" name = "second"><br>
 <input type = "submit" name = "Calculate"/> 
 </form> 
 </body> 
 </html> 

But got the same results.

1
  • I don't completely understand what it is you're asking. Also you indent code ugly. Commented Mar 23, 2014 at 3:59

2 Answers 2

1

You never bother passing around both values, just display results then prompt for a new value. You need to embed the "previous" value as a hidden form field:

<form ...>
   <input type="hidden" name="first" value="$first" />
   <input type="text" name="second" />
Sign up to request clarification or add additional context in comments.

Comments

0

First, you don't need to set your values for $second and $total inside the conditional if they are the same in both. Second, you have the ternary assignment wrapped in parenthesis, so that it sets the value to a boolean, not to the value of the $_POST array. So you have:

$second = (!empty($_POST['second']) ? $_POST['second'] : null);

when it should be:

$second = !empty($_POST['second']) ? $_POST['second'] : null;

Here is a cleaner approach:

$first = isset($_POST['Calculate']) ? $total : 2;
$second = !empty($_POST['second']) ? $_POST['second'] : 0;
$total = $first + $second;

if(isset($_POST['Calculate'])) 
{
      print "<h2>Results</h2>"; 
      print "The total of the two numbers: $first + $second = $total <p>"; 
}

6 Comments

I still get Notice: Undefined variable: total in C:\xampp\htdocs\AddingForm\index.php on line 8
Well, if you don't pass in the running total, why is your code written as if you are?
I do want to pass the running total.
So set the total as a hidden input so its always passed in, and use the post array to access it instead of assuming it is set.
Please forgive my php ignorance. Is this what you were referring to? $total = $_POST['total'] + $second;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.