用网页打开php登陆页面不提示登陆是否成功
用echo输出登陆成功或者失败的提示,但是打开网页登陆的时候点击登陆没有反应.<?php
include_once 'init.php';
if($_POST['sub']){
$username=filterstr($_POST['username']);
$password=md5(filterstr($_POST['password']));
$result=$conn->query("select * from users where username='$username' and password='$password'");
if($result->num_rows>0){
$row=$result->fetch_assoc();
if ($row['password']==$password){
echo "登录成功";
}else{
echo "账号或密码错误";
}
}else{
echo "登录失败";
}
}
?>
<html>
<head>
<title>管理员登录</title>
<style>
.login{
width: 400px;
margin: 0px auto;
}
</style>
</head>
<body>
<div class="login">
<form>
<table>
<tr><td><lable for="username">用户</lable></td><td><input type="text" name="username" id="username"></td></tr>
<tr><td><lable for="password">密码</lable></td><td><input type="password" name="password" id="password"></td></tr>
<tr><td colspan="2"><input type="submit" value="登录" name="sub"></td></tr>
</table>
</form>
</div>
</body>
</html>
form的method要标明是post才能拿到提交的内容吧 本帖最后由 shipon 于 2023-11-30 00:08 编辑
include_once 'init.php';
if ($_POST['sub']) {
$username = filterstr($_POST['username']);
$password = md5(filterstr($_POST['password']));
$result = $conn->query("select * from users where username='$username' and password='$password'");
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
if ($row['password'] == $password) {
echo "登录成功";
} else {
echo "账号或密码错误";
}
} else {
echo "登录失败";
}
}
?>
<html>
<head>
<title>管理员登录</title>
<style>
.login {
width: 400px;
margin: 0px auto;
}
</style>
</head>
<body>
<div class="login">
<form method="POST">
<table>
<tr>
<td><label for="username">用户</label></td>
<td><input type="text" name="username" id="username"></td>
</tr>
<tr>
<td><label for="password">密码</label></td>
<td><input type="password" name="password" id="password"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录" name="sub"></td>
</tr>
</table>
</form>
</div>
</body></html> 不会写的话建议看下别人开源的怎么写的登录,别自己瞎琢磨,不行网上搜搜也行。你的form的action去哪了 form表单是不是要表明post方式请求你才能那导值吧 。
表单元素设置form的action属性<?php
include_once 'init.php';
if (isset($_POST['sub'])) {
$username = filterstr($_POST['username']);
$password = md5(filterstr($_POST['password']));
$result = $conn->query("select * from users where username='$username' and password='$password'");
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
if ($row['password'] == $password) {
echo "登录成功";
} else {
echo "账号或密码错误";
}
} else {
echo "登录失败";
}
exit; // 结束脚本执行,确保只返回登录结果
}
?>
<html>
<head>
<title>管理员登录</title>
<style>
.login{
width: 400px;
margin: 0px auto;
}
</style>
</head>
<body>
<div class="login">
<form action="login.php" method="POST">
<table>
<tr><td><label for="username">用户</label></td><td><input type="text" name="username" id="username"></td></tr>
<tr><td><label for="password">密码</label></td><td><input type="password" name="password" id="password"></td></tr>
<tr><td colspan="2"><input type="submit" value="登录" name="sub"></td></tr>
</table>
</form>
</div>
<script>
function submitForm(event) {
event.preventDefault(); // 阻止表单默认提交行为
var form = event.target;
var formData = new FormData(form);
var xhr = new XMLHttpRequest();
xhr.open(form.method, form.action, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
// 在页面中显示登录结果
var response = xhr.responseText;
// TODO: 在页面中显示响应
console.log(response);
}
};
xhr.send(formData);
}
</script>
</body>
</html> 本帖最后由 萌新表示是小白 于 2023-11-30 15:54 编辑
<?php
include_once 'init.php';
?>
<html>
<head>
<title>管理员登录</title>
<style>
.login{
width: 400px;
margin: 0px auto;
}
</style>
</head>
<body>
<?php
if($_POST['sub']){
$username=filterstr($_POST['username']);
$password=md5(filterstr($_POST['password']));
$result=$conn->query("select * from users where username='$username' and password='$password'");
if($result->num_rows>0){
$row=$result->fetch_assoc();
if ($row['password']==$password){
echo "登录成功";
}else{
echo "账号或密码错误";
}
}else{
echo "登录失败";
}
}
?>
<div class="login">
<form action="login.php" method="POST" style="<?php if($_POST['sub']){ disolay:none} ?>" >
<table>
<tr><td><lable for="username">用户</lable></td><td><input type="text" name="username" id="username"></td></tr>
<tr><td><lable for="password">密码</lable></td><td><input type="password" name="password" id="password"></td></tr>
<tr><td colspan="2"><input type="submit" value="登录" name="sub"></td></tr>
</table>
</form>
</div>
</body>
</html>
php的echo打印要写在body里面,method设置为POST,
这里我还帮你添加了个,如果登录了就将登录框隐藏的功能
{:17_1067:} 感谢大家,问题已经解决,就是form后面没加method="post",感谢.
页:
[1]