php Form and action in the same page

forms, php

Solution

You can put the code in the same file and change the form to:

<form action="" method="post"> 

or

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 

to submit it to the same page.

Problem

I have a form like this ``` <html> <body> <form action="calc.php" method="post"> Primo numero: <input type="text" name="a" /></br> Operazione (+-*/): <input type="text" name="b" /></br> Secondo numero: <input type="text" name="c" /> <input type="submit" /> </form> </body> </html> ``` lied to a separate .php file named calc.php that is like ``` <?php echo $_POST["a"]; ?><?php echo $_POST["b"]; ?><?php echo $_POST["c"]; ?><strong>=</strong> <?php if ($_POST["b"] == "+") { echo $_POST["a"] + $_POST["c"]; } if ($_POST["b"] == "-") { echo $_POST["a"] - $_POST["c"]; } if ($_POST["b"] == "*") { echo $_POST["a"] * $_POST["c"]; } if ($_POST["b"] == "/") { echo $_POST["a"] / $_POST["c"]; } ?> ``` It works fine but I'd like to show the result in the same page How can I do?

Original source