【问题标题】:Undefined index text input php? [duplicate]未定义的索引文本输入php? [复制]
【发布时间】:2017-11-12 13:49:33
【问题描述】:

我想从 php web 文件获取文本输入值以将值保存到数据库,但文本输入不适用于 php 文件。并显示此错误:

注意:未定义索引:C:/......第 7 行中的 txtUsername 注意:未定义索引:C:/......第8行中的txtPassword

我的html:

<form  action="../Controller/LoginController.php">
    <input type="text" id="txtUsername" />
    <input type="text" id="txtPassword"  />
    <input type="submit" name="btnLogin" value="login" />
</form>

我的 php 文件:

$strUsername = $_POST["txtUsername"];  //line 7
$strPassword = $_POST["txtPassword"];  //line 8

【问题讨论】:

    标签: php mysql file input dao


    【解决方案1】:

    表单输入 ID 不能用于获取值。您需要设置name 属性。

    像这样:

    <input type="text" name="txtUsername" /> // Notice the name attribute
    

    然后就可以正常获取值了。

    $strUsername = $_POST["txtUsername"];
    

    【讨论】:

    • 嘿,投反对票的人,想解释一下投反对票的原因吗?
    【解决方案2】:

    您在 PHP 代码中通过 $_POST 引用它,但没有通过 HTML 中的 POST 专门传递它。

    您还需要指定 NAME 参数,因为这是 PHP 引用的,而不是 ID

    试试这个:

    <form action="../Controller/LoginController.php" method="post">
        <input type="text" id="txtUsername" name="txtUsername" />
        <input type="text" id="txtPassword" name="txtPassword"  />
        <input type="submit" name="btnLogin" value="login" />
    </form>
    

    【讨论】:

      猜你喜欢
      • 2018-09-24
      • 2018-10-16
      • 2011-06-18
      • 2014-01-27
      • 2014-06-24
      • 1970-01-01
      • 2013-05-22
      • 1970-01-01
      相关资源
      最近更新 更多