PHP$\u POST获取未定义的索引,即使提供了输入

PHP$\u POST获取未定义的索引,即使提供了输入,php,post,Php,Post,我正在尝试使用$\u POST方法从HTML表单获取用户名和密码, 但是我得到了一个给定输入的未定义索引错误 我的索引文件的表单部分如下 <form class="form-signin" role="form"action="" method="post"> <h2 class="form-signin-heading">title<em class='current'>title</em></h2>

我正在尝试使用$\u POST方法从HTML表单获取用户名和密码, 但是我得到了一个给定输入的未定义索引错误

我的索引文件的表单部分如下

<form class="form-signin"  role="form"action="" method="post">
        <h2 class="form-signin-heading">title<em class='current'>title</em></h2>
        <input id="username" type="username" class="form-control" placeholder="Username" required autofocus>
        <input id="password" type="password" class="form-control" placeholder="Password" required>
        <input name ="submit" type="submit" value="sign in">
      </form>
我的索引文件顶部还包含了名为auth.php的php脚本

这是我的auth.php脚本代码示例

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
echo "auth is called";
if(isset($_POST['submit'])){
echo "submit is pressed";
//this if statement enters when submit is pressed
if(empty($_POST['username']) || empty($_POST['password'])) {
echo "Username or Password is empty";
//this if statement enters too, even with input given
}
else{
echo "YAY";
}
}
?>
“用户名或密码为空”语句不断打印,即使我在输入字段中输入用户名和密码也是如此。输入字段中缺少name属性有什么问题,应该是

编辑2:用户名输入中缺少类型=文本

输入字段中缺少name属性,应为

编辑2:用户名输入中缺少类型=文本

将用户名输入的类型设置为文本:

将用户名输入的类型设置为文本:


改正你的表格,1。名字不见了,2。没有用户名为的数据类型

<form class="form-signin" role="form" action="" name="loginform" method="post">
    <h2 class="form-signin-heading">title<em class='current'>title</em></h2>
    <input id="username" name="username" type="text" class="form-control" placeholder="Username" required autofocus>
    <input id="password" name="password" type="password" class="form-control" placeholder="Password" required>
    <input name="submit" type="submit" value="sign in">
</form>

改正你的表格,1。名字不见了,2。没有用户名为的数据类型

<form class="form-signin" role="form" action="" name="loginform" method="post">
    <h2 class="form-signin-heading">title<em class='current'>title</em></h2>
    <input id="username" name="username" type="text" class="form-control" placeholder="Username" required autofocus>
    <input id="password" name="password" type="password" class="form-control" placeholder="Password" required>
    <input name="submit" type="submit" value="sign in">
</form>
输入类型=需要文本和名称属性

如果没有获得所需的输出,请使用print_r或var_dump检查值

这将帮助您检查代码的哪一部分出现故障。如果你这样做了,你会发现只有在你的html表单内容中有错误,因为你不会打印任何字段内容,你会很容易地把它整理出来

输入类型=需要文本和名称属性

<input id="username" name="username" type="username" class="form-control" placeholder="Username" required autofocus>
            <input id="password" type="password" name="password" class="form-control" placeholder="Password" required>

    <?php
    ini_set('display_errors',1);
    error_reporting(E_ALL);
    echo "auth is called";
    if(isset($_POST['submit'])){
    echo "submit is pressed";
    //this if statement enters when submit is pressed
    if(empty($_POST['username']) || empty($_POST['password'])) {
    echo "Username or Password is empty";
    //this if statement enters too, even with input given
    }
    else if(empty($_POST['username']){
      echo "Username  is empty";
    }
    else if(empty($_POST['password']){
      echo "Password  is empty";
    }
    else{
    echo "YAY";
    }
    }
    ?>
如果没有获得所需的输出,请使用print_r或var_dump检查值


这将帮助您检查代码的哪一部分出现故障。如果你这样做了,你会发现只有在你的html表单内容中有错误,因为你不会打印任何字段内容,你会很容易地把它整理出来

此外,没有输入类型用户名。它应该是文本。@CodingAnt您有两种属性类型。在第一次输入!此外,没有输入类型用户名。它应该是文本。@CodingAnt您有两种属性类型。在第一次输入!我没有在表单中定义输入名称,不确定我怎么会错过它。谢谢大家的帮助!我没有在表单中定义输入名称,不确定我怎么会错过它。谢谢大家的帮助!
<input id="username" name="username" type="username" class="form-control" placeholder="Username" required autofocus>
            <input id="password" type="password" name="password" class="form-control" placeholder="Password" required>

    <?php
    ini_set('display_errors',1);
    error_reporting(E_ALL);
    echo "auth is called";
    if(isset($_POST['submit'])){
    echo "submit is pressed";
    //this if statement enters when submit is pressed
    if(empty($_POST['username']) || empty($_POST['password'])) {
    echo "Username or Password is empty";
    //this if statement enters too, even with input given
    }
    else if(empty($_POST['username']){
      echo "Username  is empty";
    }
    else if(empty($_POST['password']){
      echo "Password  is empty";
    }
    else{
    echo "YAY";
    }
    }
    ?>
<input id="username" name="username" type="username" class="form-control" placeholder="Username" required autofocus>
            <input id="password" type="password" name="password" class="form-control" placeholder="Password" required>

    <?php
    ini_set('display_errors',1);
    error_reporting(E_ALL);
    echo "auth is called";
    if(isset($_POST['submit'])){
    echo "submit is pressed";
    //this if statement enters when submit is pressed
    if(empty($_POST['username']) || empty($_POST['password'])) {
    echo "Username or Password is empty";
    //this if statement enters too, even with input given
    }
    else if(empty($_POST['username']){
      echo "Username  is empty";
    }
    else if(empty($_POST['password']){
      echo "Password  is empty";
    }
    else{
    echo "YAY";
    }
    }
    ?>