【问题标题】:Can't access form variable with php无法使用 php 访问表单变量
【发布时间】:2014-08-30 19:03:12
【问题描述】:

我是 Web 开发新手,一直在搞乱BootStrap。我一直在尝试在我的新网站上创建一个 Web 表单,但我似乎无法访问用户名变量。

这是我的表单代码:

    <form class="navbar-form navbar-left" role="search" action="username">
        <div class="form-group">
            <input name="username" type="text" class="form-control" placeholder="Username">
        </div>
        <button type="submit" class="btn btn-default">Search</button>
    </form>

这是我在用户名文件夹中的index.php

    <?php
        echo $_POST['username'];
        echo 'fail';
        echo 'username';
        if(isset($_POST['username'])) {
            $name = $_POST['username'];
            echo 'success';
        }
    ?>

通过我的调试,我得到消息无法显示,但没有显示用户名,所以我认为我在设置用户名或访问用户名时做错了。

【问题讨论】:

  • 表单中action 属性的值错误。它应该是存储 PHP 代码的文件的名称。此外,您还没有指定method 属性,最好是post

标签: php html twitter-bootstrap input


【解决方案1】:

根据您的描述,您的 HTML 代码应如下所示:

<form method="post" class="navbar-form navbar-left" role="search" action="username/index.php">
    <div class="form-group">
        <input name="username" type="text" class="form-control" placeholder="Username">
    </div>
    <button type="submit" class="btn btn-default">Search</button>
</form>

您也可以使用&lt;input class="btn btn-default" type="submit" value="Search"/&gt; 代替&lt;button type="submit"&gt;&lt;/button&gt;

【讨论】:

    【解决方案2】:

    你有以下错误

    1. 您没有在表单中设置method 属性。因此,当表单提交时,数据将通过get 方法发布。您需要访问$_GET 变量以获取表单数据。因此,当您提交表单时,数据将位于$_GET 变量中。但是您正尝试在代码中访问$_POST 变量。因此您可以尝试在表单中将方法属性设置为method="post 并继续index.php 中的相同代码或尝试将 index.php 中的 $_POST 更改为 $_GET` 并且在您的表单中不进行任何更改

    2. 你在提交表单时定义index.php中的代码,所以你需要在你的表单中设置action="index.php"

    所以最终会是这样的

    <form class="navbar-form navbar-left" role="search" action="username/index.php" method="post">
        <div class="form-group">
            <input name="username" type="text" class="form-control" placeholder="Username">
        </div>
        <button type="submit" class="btn btn-default">Search</button>
    </form>
    

    index.php

     <?php
            echo $_POST['username'];
            echo 'fail';
            echo 'username';
            if(isset($_POST['username'])) {
                $name = $_POST['username'];
                echo 'success';
            }
        ?>
    

    <form class="navbar-form navbar-left" role="search" action="username/index.php">
        <div class="form-group">
            <input name="username" type="text" class="form-control" placeholder="Username">
        </div>
        <button type="submit" class="btn btn-default">Search</button>
    </form>
    

    index.php

    <?php
        echo $_GET['username'];
        echo 'fail';
        echo 'username';
        if(isset($_GET['username'])) {
            $name = $_GET['username'];
            echo 'success';
        }
    ?>
    

    【讨论】:

    • 如果 username 是 OP 所说的文件夹,它应该是 action="/username/"
    【解决方案3】:

    如果您使用$_POST['username'] 访问用户名,请将您的action 属性设置为index.php 并设置您的method="post"

    【讨论】:

      猜你喜欢
      • 2019-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-11
      • 1970-01-01
      • 1970-01-01
      • 2016-11-12
      • 2012-03-06
      相关资源
      最近更新 更多