php Form and action in the same page

2019-07-23 20:05发布

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?

标签: php forms
1条回答
成全新的幸福
2楼-- · 2019-07-23 20:47

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.

查看更多
登录 后发表回答