given the buttons PLUS and MINUS

2 lines of the number

RESULT button

string with Result

how to write, so after I entered 2 numbers, clicked PLUS, I got the same output (that is, the forms are filled, except for the result), but php realized that I clicked plus, and only then I PRESS the result and the RESULT in the STRING ( type = "text")?

  • five
    When I started ovnododit, it never occurred to me to make a calculator ... IMHO is not a good practice! - Palmervan

2 answers 2

It is necessary that the data that came from the form be inserted back into the form.

 if(isset($_POST['a']) && isset($_POST['b'])){?> <input name='a' value='<?=$_POST['a']?>'> <input name='b' value='<?=$_POST['b']?>'> <?} 

PS: a, b are the names of the addends, substitute your names for them.

    "but php realized that I clicked a plus ..." I am afraid that here I must understand not PHP, but you must understand how the interaction between the client and server parts of the application occurs.

    The PHP script will accept the variable (with the sign of addition, subtraction, etc.) and if it doesn’t have further instructions, it will cheerfully and easily complete its work, forgetting everything that was passed to it. Accordingly, it is necessary to either save this variable somehow (session, database, etc.), or send it back to the user on the screen and take it into account when re-requesting.

    How to determine that it was pressed "PLUS", and not the calculation of the result, it is simple:

     <form action="test.php" method="POST"> ... <p><input type="submit" name="action" value="+"></p> <p><input type="submit" name="action" value="-"></p> <p><input type="submit" name="action" value="рассчитать"></p> ... </form> 

    A form can contain several buttons of the "submit" type, and when a certain button is clicked, it will transmit exactly its value. That is, if the "+" button is pressed in this form, in the "test.php" script the value of $ _REQUEST ['action'] will be equal to "+"; if the button "calculate" is pressed - $ _REQUEST ['action'] will be equal to "calculate", etc.

    Well, how to understand what kind of action I was told. The rest yourself.

    Criticism of the answer welcome.


    And this is all under the condition, if at all, there is a need to notify the server in advance about the upcoming manipulations with numbers. Otherwise, the sign can be substituted with js in a hidden field and sent to the server directly when you click the "calculate" button.