在表单提交上调用特定的PHP函数

Pik*_*uha 33 php isset

我试图在提交表单时调用特定的php函数,表单和php脚本都在同一页面中.我的代码在下面.(它不起作用,所以我需要帮助)

<html>
    <body>
    <form method="post" action="display()">
        <input type="text" name="studentname">
        <input type="submit" value="click">
    </form>
    <?php
        function display()
        {
            echo "hello".$_POST["studentname"];
        }
    ?>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

The*_*pha 61

在以下行中

<form method="post" action="display()">
Run Code Online (Sandbox Code Playgroud)

动作应该是你的脚本的名称,你应该调用函数,像这样的东西

<form method="post" action="yourFileName.php">
    <input type="text" name="studentname">
    <input type="submit" value="click" name="submit"> <!-- assign a name for the button -->
</form>

<?php
function display()
{
    echo "hello ".$_POST["studentname"];
}
if(isset($_POST['submit']))
{
   display();
} 
?>
Run Code Online (Sandbox Code Playgroud)


kus*_*shi 8

你不需要这个代码

<?php
function display()
{
echo "hello".$_POST["studentname"];
}
?>
Run Code Online (Sandbox Code Playgroud)

相反,您可以通过使用检查post变量来检查表单是否已提交isset.

这里是代码

if(isset($_POST)){
echo "hello ".$_POST['studentname'];
}
Run Code Online (Sandbox Code Playgroud)

点击这里查看isset的php手册


Her*_*uez 5

假设您的脚本名为x.php,请尝试此操作

<?php 
   function display($s) {
      echo $s;
   }
?>
<html>
    <body>
        <form method="post" action="x.php">
            <input type="text" name="studentname">
            <input type="submit" value="click">
        </form>
        <?php
           if($_SERVER['REQUEST_METHOD']=='POST')
           {
               display();
           } 
        ?>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)