我正在尝试使用$ _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>
我的php脚本(名为auth.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";
}
}
?>
“用户名或密码为空”语句不断打印,即使我在输入字段中输入用户名和密码也是如此。有什么问题
答案 0 :(得分:5)
缺少&#34;名称&#34; attribute in input
字段,应为
编辑2:在用户名input
<input type="text" name='username' id="username" class="form-control" placeholder="Username" required autofocus>
<input name='password' id="password" type="password" class="form-control" placeholder="Password" required>
答案 1 :(得分:0)
将username
输入的类型设置为"text"
:
<input name='username' id="username" type="text" class="form-control" placeholder="Username" required autofocus>
<input name='password' id="password" type="password" class="form-control" placeholder="Password" required>
答案 2 :(得分:0)
更正您的表单,1。名称缺失,2。没有名称为username
的数据类型<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>
答案 3 :(得分:0)
输入 type = text
和 name attr
<input name='username' id="username" type="text" class="form-control" placeholder="Username" required autofocus>
如果您没有获得所需的输出,请使用print_r()
或var_dump
检查值。
这将帮助您检查代码的哪个部分出现故障。如果你已经这样做了,你会发现只有你的html表单内容有问题,因为你不会打印任何字段内容,你可以很容易地将其整理出来......
答案 4 :(得分:0)
<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";
}
}
?>