简体   繁体   中英

how to print all post results when a form is submitted

What is wrong with this code?? It doesn't echo the answer.

<?php
     $num1=$_POST['fnum'];
     $num2=$_POST['snum'];

     $sum=$num1 + $num2;
     $diff=$num1 - $num2;
     $prod=$num1 * $num2;
     $quo=$num1 / $num2;
     $mod=$num1 % $num2;


     echo "The sum is " .$sum. <br>;
     echo "The differense is " .$diff. <br>;
     echo "The product is " .$prod. <br>;
     echo "The quotient is " .number_format($quo,2). <br>;
     echo "The remainder is " .$mod. <br>;
  ?>    

You are concatenating the <br> tag in wrong way.

echo "The sum is " .$sum. <br>;

It should be:

echo "The sum is " .$sum. "<br>";

Assuming the numbers 10 and 5 taken from a form using a POST method:

Gave the following results: (from the fixed version found below)

The sum is 15
The differense is 5
The product is 50
The quotient is 2.00
The remainder is 0

You were missing quotes for the concatenates.

Ie:

echo "The sum is " .$sum.  <br> ;
             missing "    ^    ^

and the others as well.

This works with no parse errors:

<?php
     $num1=$_POST['fnum'];
     $num2=$_POST['snum'];

     $sum=$num1 + $num2;
     $diff=$num1 - $num2;
     $prod=$num1 * $num2;
     $quo=$num1 / $num2;
     $mod=$num1 % $num2;

     echo "The sum is " .$sum. "<br>";
     echo "The differense is " .$diff. "<br>";
     echo "The product is " .$prod. "<br>";
     echo "The quotient is " .number_format($quo,2). "<br>";
     echo "The remainder is " .$mod. "<br>";
?>

It could also be done like this, giving the same output format:

<?php
     $num1=$_POST['fnum'];
     $num2=$_POST['snum'];

     $sum=$num1 + $num2;
     $diff=$num1 - $num2;
     $prod=$num1 * $num2;
     $quo=$num1 / $num2;
     $mod=$num1 % $num2;

     echo "The sum is " .$sum;
     echo "<br>";
     echo "The differense is " .$diff;
     echo "<br>";
     echo "The product is " .$prod;
     echo "<br>";
     echo "The quotient is " .number_format($quo,2);
     echo "<br>";
     echo "The remainder is " .$mod;
?>

It'd be good if we could see the code you're using to post to the PHP. From a quick scan of what you've put, it seems that may be your issue. Make sure you're posting with the names fnum and snum in a way that you can then use in calculations. I'd suggest wrapping the inputs as follows:

$num1 = floatval($_POST['fnum']);
$num2 = floatval($_POST['snum']);

That'll ensure whatever you're pulling in is a number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM