1

for example, I receive two inputs value1 and value2 and I want this input for different functions. Like addition, subtraction and multiplication.

my code

<?php
$x = $_POST['fnum'];
$y = $_POST['snum'];

if (isset($_POST['add'])) {
  $sum = $x + $y;
  echo "Result:<input type='text' value='$sum'/>";
}
if (isset($_POST['sub'])) {
  $sub = $x - $y;
  echo "Result:<input type='text' value='$sub'/>";
}
if (isset($_POST['mul'])) {
  $mul = $x * $y;
  echo "Result:<input type='text' value='$mul'/>";
}

<body>
  <form method="post">
    Enter first number <input type="text" name="fnum" />
    <hr />
    Enter second number <input type="text" name="snum" />
    <hr />
    <input type="submit" name="add" value="ADD" />
    <input type="submit" name="sub" value="Subtract" />
    <input type="submit" name="mul" value="Multiply" />
  </form>
</body>

In this it is asking me to feed input for each operation separately

8
  • You can use session variable to store last input values and print them in the form as default values for fnum & snum Commented Jun 16, 2021 at 6:08
  • You can use GET method instead of POST Commented Jun 16, 2021 at 6:09
  • 1
    It's not clear what the use case actually is. If you want to do all the operations simultaneously the replace the buttons with a set of checkboxes indicating which calculations you want to perform, and have a single submit button. Then adjust the PHP code so it does all the selected calculations and outputs all the results. But if you simply want the same values to show up again in the form before you submit it again, then just use PHP to echo them back into the inputs Commented Jun 16, 2021 at 6:44
  • 1
    Another idea, why not just do all this with JavaScript? There's really no need for server-side code or postbacks or anything, just to make a simple calculator Commented Jun 16, 2021 at 6:55
  • 1
    Instead of <input type="text" /> you could use <input type="number" /> Commented Jun 16, 2021 at 9:50

1 Answer 1

2

Good, Just use the posted value in your form input like -

Enter first number <input type="text" name="fnum" value="<?php echo @$_POST['fnum'];?>"/><hr/>
Enter second number <input type="text" name="snum"  value="<?php echo @$_POST['snum'];?>"/><hr/>

So that user don't need to put the same value again and when press the other buttons the form will automatically submit with the previous values.

Note: Remember, you need to check all the posted value is set or not and use proper conditions of POST method. Have a look at the given example of your problem as solution, I give it here to give you a proper guide.

Example:

<?php 
$x = 0;
$y = 0;
if(isset($_POST['submit'])) {
    $x = $_POST['fnum'];      
    $y = $_POST['snum'];
    $operator = "";
    if($_POST['submit'] == 'ADD') {
        $operator = "+";
    } elseif($_POST['submit'] == 'Subtract') {
        $operator = "-";
    } elseif($_POST['submit'] == 'Multiply') {
        $operator = "*";
    }
    $result = $x . $operator . $y;
}?>

Your form will be-

<form method="post">
    Enter first number <input type="text" name="fnum" value="<?php echo $x;?>"/><hr/>
    Enter second number <input type="text" name="snum" value="<?php echo $y;?>"/><hr/>              
    <input type="submit"  name="submit" value="ADD"/>
    <input type="submit" name="submit" value="Subtract"/>
    <input type="submit" name="submit" value="Multiply"/>
 </form>

Result:

<input type='text' value='<?php echo (isset($result) ? $result : "-";)?>'/>
Sign up to request clarification or add additional context in comments.

4 Comments

Only thing I'd say is you need to check whether the $_POST value is set before trying to echo it, otherwise you'll get a warning when the page first loads, about the value not existing.
Warning: Undefined array key "fnum" in C:\xampp\htdocs\mytestbase\tesppage.php on line 2 Warning: Undefined array key "snum" in C:\xampp\htdocs\mytestbase\tesppage.php on line 3
@zapperlu, have a look at the example, the complete code rebuild for your better understanding.
You sure can use the @ operator but you should avoid it and instead write proper code (use isset() like you did further down in the other code snippets)

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.