【问题标题】:HTML/PHP form is not sending any POST dataHTML/PHP 表单未发送任何 POST 数据
【发布时间】:2014-09-23 04:23:02
【问题描述】:

在开发注册页面时,我遇到了一个非常恼人的问题。 通过提交按钮提交表单时,我可以看到它像“user=myusername”一样发送到浏览器,但 PHP 部分根本没有接收到它。 我尝试通过“echo”和“print_r”进行调试,但它们都显示为空,但是我从 zPanel 获取错误日志。

这是代码的 PHP 部分:

    if(isset($_GET['user']) && isset($_GET['pw']) && isset($_GET['mail']))
{
    $username = $_POST["user"];
    $password = $_POST['pw'];
    $email = $_POST["mail"];
    $ipaddress = $_SERVER["REMOTE_ADDR"];

    if(empty($username) || empty($password) || empty($email)) header("Location: register.php?empty");
    else if(user_exists($username)) header("Location: register.php?exist");
    else if(email_exists($email)) header("Location: register.php?mailexist");
    else if($password != $repeat_pass) header("Location: register.php?wrongpw");
    else
    {
        register($username,$password,$ipaddress,$email);
        header("Location: register.php?registrationsuccess");
        exit();

    }
}

这里是触发上述内容的 HTML 部分:

<form>
                <div>
                    <label for="username" class="control-label" style="color: #666; position: absolute; top: 110px;">Username</label>
                    <div class="input-group" style="margin-left: 100px; margin-top: 8px; width: 300px;">
                        <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
                        <input type="text" name="user" class="form-control" placeholder="Username" required>
                    </div>
                    <br/>
                    <label for="password" class="control-label" style="color: #666; position: absolute; top: 163px;">Password</label>
                    <div style="margin-left: 100px; width: 300px;" class="input-group">
                      <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>
                      <input type="password" name="pw" class="form-control" placeholder="Password" required>
                    </div>
                    </br>
                    <label for="email" class="control-label" style="color: #666; position: absolute; top: 220px;">Email</label>
                    <div style="margin-left: 100px; width: 300px;" class="input-group">
                      <span class="input-group-addon"><span class="glyphicon glyphicon-import"></span></span>
                      <input type="text" name="mail" class="form-control" placeholder="E-mail" required>
                    </div>
                    <div class="btn-group" style="position: absolute; top: 265px; left: 125px;">
                    <button type="submit" class="btn btn-default" style="width: 300px; font-size: 18px;">Register</button>
                </form>

是的,这是使用引导程序。 现在,每当您尝试登录时,它实际上会将您重定向到 register.php?empty,说明您没有在字段中输入任何内容。

这就是 zPanel 对我的吐槽:

[error] PHP Notice:  Undefined index: username

[错误] PHP 注意:未定义索引:密码 [错误] PHP 注意:未定义索引:电子邮件

老实说,我不知道是什么造成了所有这些麻烦。 我错过了什么吗?

【问题讨论】:

    标签: php forms post


    【解决方案1】:

    首先你没有清除表单的方法。应该是&lt;form method="POST"&gt;

    之后你需要改变

    if(isset($_GET['user']) && isset($_GET['pw']) && isset($_GET['mail']))
    

    if(isset($_POST['user']) && isset($_POST['pw']) && isset($_POST['mail']))
    

    我认为这会奏效。告诉我。

    谢谢。

    【讨论】:

    • 有趣。当我第一次设置 PHP 部分时,$_POST 详细信息根本不起作用(它只是用所有 'user=user' 名称等刷新了页面。现在突然它起作用了,我想'form method' 是它的原因。这解决了它,我欠你一个。亲切的问候。
    • 很高兴能帮到你。问候。 :)
    【解决方案2】:

    像这样更改代码...

        if(isset($_GET['user']) && isset($_GET['pw']) && isset($_GET['mail']))
    {
        $username = $_GET["user"];
        $password = $_GET['pw'];
        $email = $_GET["mail"];
        $ipaddress = $_SERVER["REMOTE_ADDR"];
    
        if(empty($username) || empty($password) || empty($email)) header("Location: register.php?empty");
        else if(user_exists($username)) header("Location: register.php?exist");
        else if(email_exists($email)) header("Location: register.php?mailexist");
        else if($password != $repeat_pass) header("Location: register.php?wrongpw");
        else
        {
            register($username,$password,$ipaddress,$email);
            header("Location: register.php?registrationsuccess");
            exit();
    
        }
    }
    

    HTML 应该是这样的...

    <form method="GET" action="action_page_name.php">
                    <div>
                        <label for="username" class="control-label" style="color: #666; position: absolute; top: 110px;">Username</label>
                        <div class="input-group" style="margin-left: 100px; margin-top: 8px; width: 300px;">
                            <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
                            <input type="text" name="user" class="form-control" placeholder="Username" required>
                        </div>
                        <br/>
                        <label for="password" class="control-label" style="color: #666; position: absolute; top: 163px;">Password</label>
                        <div style="margin-left: 100px; width: 300px;" class="input-group">
                          <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>
                          <input type="password" name="pw" class="form-control" placeholder="Password" required>
                        </div>
                        </br>
                        <label for="email" class="control-label" style="color: #666; position: absolute; top: 220px;">Email</label>
                        <div style="margin-left: 100px; width: 300px;" class="input-group">
                          <span class="input-group-addon"><span class="glyphicon glyphicon-import"></span></span>
                          <input type="text" name="mail" class="form-control" placeholder="E-mail" required>
                        </div>
                        <div class="btn-group" style="position: absolute; top: 265px; left: 125px;">
                        <button type="submit" class="btn btn-default" style="width: 300px; font-size: 18px;">Register</button>
                    </form>
    

    在进行更多 PHP 开发之前,您应该了解 GET 和 POST 之间的区别。

    Visit a Website like this

    【讨论】:

    • 您的代码中有更多错误。您没有在 header() 函数中传递值。您必须为要传递给 register.php 页面的变量赋值。
    【解决方案3】:

    在 PHP-HTML 中提交数据时,您必须使用 '&lt;form&gt; 的方法属性。 method属性指定如何发送form-data(form-data发送到action属性指定的页面)。

    表单数据可以作为 URL 变量(使用 method="get")或作为 HTTP 后事务(使用 method="post")发送。在&lt;form&gt; 中,您必须提到 GET/POST 方法,例如

    <form method="GET">
    

    此外,如果您将数据提交到另一个页面,则应设置action 属性。否则action=""

     <form method="GET" action="actionpage.php">
    

    【讨论】:

    • 由于没有提供方法,默认值已经是GET
    【解决方案4】:

    您的表单缺少方法和操作。动作是将接收和处理数据的文件。方法是表单将如何发送数据。

    <form method="POST" action="file.php">
    

    【讨论】:

      【解决方案5】:

      你应该为按钮submit添加name

      <input type="submit" name="submit">
      

      之后,勾选isset提交按钮:

      if (isset($_POST['submit']) {
          //enter code here
      }
      

      【讨论】:

        猜你喜欢
        • 2013-12-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-10
        相关资源
        最近更新 更多