首页 > 解决方案 > 为什么点击 input[type='button'] 时 PHP 无法识别?

问题描述

我只想简单地点击echo 'OK!'输入时type="button" name="calculateBtn",但PHP无法识别该条件if (isset($_POST['calculateBtn'])),所以不会做任何事情,有人可以告诉我这是为什么吗?

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<div class="container">
    <div class="row">
        <form action="" method="post">
            <div>
                <input id="input" type="text">
                <?php
                if (isset($_POST['calculateBtn'])) {
                    echo 'ok';
                }
                ?>
                <input name="calculateBtn" value="change the value" type="button">
            </div>
        </form>
    </div>
</div>
</body>
</html>

标签: phpisset

解决方案


首先,您没有在表单中定义动作,其次按钮类型设置为按钮而不是提交,它不会提交表单。

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<div class="container">
    <div class="row">
        <form action="page.php" method="post">
            <div>
                <input id="input" type="text">
                <?php
                if (isset($_POST['calculateBtn'])) {
                    echo 'ok';
                }
                ?>
                <input name="calculateBtn" value="change the value" type="submit">
            </div>
        </form>
    </div>
</div>
</body>
</html>

请将 page.php 设置为您的页面(文件)的名称。


推荐阅读