2

I have the following code for a simple calculator. The code works fine, but I want the value in the dropdown list to stay there after submitted. How would I do this? Essentially, I want the operator to stay selected once the calculation has been done. At the moment, it just shows a '+', even if the sum is 25 / 5.

<?php 

$number1 = $_POST['number1'];
$number2 = $_POST['number2'];
$operation = $_POST['operator'];


Switch ($operation) {
case 'add': $answer = $number1 + $number2;
break;
case 'minus': $answer = $number1 - $number2; 
break;
case 'divide': $answer = $number1 / $number2; 
break;
case 'multiply': $answer = $number1 * $number2;
break;
}


?>

<form name='calculator' method='post' action=''>
<table>
    <tr>
        <td>
            <input name="number1" type="text" value="<?php     i    if(isset($_POST['number1'])) { echo  htmlentities($_POST['number1']);}?>"         />
        </td>
        <td>
            <select name="operator">
                <option value="add">+</option>
                <option value="minus">-</option>
                <option value="divide">/</option>
                <option value="multiply">x</option>
            </select>
        </td>
        <td>
            <input name="number2" type="text" value="<?php     if(isset($_POST['number2'])) { echo  htmlentities($_POST['number2']);}?>"     />
        </td>
        <td>    
            <input name="submit" type="submit" value="=" />
        </td>
        <td>        
            <input name="" type="text" value="<?php echo $answer ?>" />
        </td>
    </tr>
</table>

Tatws24
  • 107
  • 1
  • 2
  • 10

1 Answers1

10

You have to set the selected attribute for the option that was submitted. So you have to check the submitted value for each option. In my solution I am using the ternary operator to echo the selected attribute only for the correct operator.

<select name="operator">
    <option value="add" <?php echo (isset($_POST['operator']) && $_POST['operator'] == 'add') ? 'selected' : ''; ?>>+</option>
    <option value="minus" <?php echo (isset($_POST['operator']) && $_POST['operator'] == 'minus') ? 'selected' : ''; ?>>-</option>
    <option value="divide" <?php echo (isset($_POST['operator']) && $_POST['operator'] == 'divide') ? 'selected' : ''; ?>>/</option>
    <option value="multiply" <?php echo (isset($_POST['operator']) && $_POST['operator'] == 'multiply') ? 'selected' : ''; ?>>x</option>
</select>

The code above is somewhat repetitive. It keeps repeating a lot of code and html. It would be great if we could factor out the repetitive stuff. Luckily we can do that by creating an array that stores the options and loop through them using a foreach, like this:

<?php
$options = [
    'add' => '+',
    'minus' => '-',
    'divide' => '/',
    'multiply' => 'x'
];
?>

<select name="operator">
    <?php foreach ($options as $key => $label) { ?>
        <option value="<?= $key ?>" <?= (isset($_POST['operator']) && $_POST['operator'] == $key) ? 'selected' : '' ?>><?= $label ?></option>
    <?php } ?>
</select>
Cave Johnson
  • 6,499
  • 5
  • 38
  • 57
  • Brilliant. I will try this when I get home. Thank you. – Tatws24 Mar 18 '16 at 08:02
  • great solution i must say – Usman Iqbal May 06 '17 at 15:14
  • How should I retain the same value if I submit another form on the same page? E.g. Select value from dropdown1 and submit1. It generates dropdown2 I again select value from dropdown2 and submit2 and it generates dropdown3. But on submit2 I loose dropdown1. – Ravi Sharma Jun 09 '20 at 04:54
  • @RaviSharma so all 3 dropdowns are on the same page? If you make sure to use the above solution for each dropdown, it should keep the values each time you submit. – Cave Johnson Jun 09 '20 at 05:00
  • Yes all the dropdown are on the same page. Actually on submit each drop down it also generate a list for that dropdown value. – Ravi Sharma Jun 09 '20 at 06:22
  • @RaviSharma If you put your code on pastebin here: https://pastebin.com/, I can take a look for you to see if I can give you advice. Put your code and click "Create New Paste" then copy and paste the url into a comment. – Cave Johnson Jun 09 '20 at 15:35