PHP脚本不会抓取HTML表单输入

时间:2015-11-14 06:08:44

标签: php html

当我在填写名称和邮件ID列后点击提交按钮时,在这些字段中输入的数据不会显示在php页面中。

HTML代码: -

<html>
<body>

<form action="data.php" method="post">
Name: <input type="text" name="name" /><br>
E-mail: <input type="text" name="email"/><br>
<input type="submit">
</form>

</body>
</html>

PHP代码: -

Welcome <?php echo $_REQUEST["name"]; ?><br>
    Your email address is: <?php echo $_POST["email"]; ?>

2 个答案:

答案 0 :(得分:1)

像这样试试;

HTML

    <body>
        <form action="data.php" method="post">
            Name: <input type="text" name="name" /><br>
            E-mail: <input type="text" name="email"/><br>
            <input type="submit">
        </form>

    </body>
</html>

PHP

if ($_SERVER['REQUEST_METHOD'] === 'POST'){
    if(isset($_POST["name"]) && isset($_POST["email"])){
        $name = $_POST["name"];
        $email = $_POST["email"];
        if(!empty($name) && !empty($email){
            echo "Your name is $name with the email : $email";
        } else {
            echo "Name or Email is not set";
        }
    }
}

答案 1 :(得分:-2)

这个测试代码对我有用

<html>
<body>
<form action="" method="post">
Name: <input type="text" name="name" /><br>
E-mail: <input type="text" name="email"/><br>
<input type="submit" name="sub">
</form>
<?php
if(isset($_POST['sub'])){?>
Welcome <?php echo $_REQUEST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
<?php }?>
</body>
</html>

它也可以在单独的data.php文件中使用php