gpt4 book ai didi

javascript - PHP 验证表单

转载 作者:行者123 更新时间:2023-11-30 08:40:10 27 4
gpt4 key购买 nike

我一直在尝试从我的 login.php 中获取验证消息以显示在带有表单的页面上,我哪里出错了?我曾尝试过 JavaScript 来显示信息,但没有成功,它一直显示如下图所示。

我知道它可以添加到同一页面以获得我想要的结果,但我正在尝试将其作为一个干净的页面来做,没有 jQuery,最多只有一些普通的 JavaScript。目的是在表单上方显示验证消息,起初我想的是跨度,但我不确定在哪里可以找到有关如何在同一页面上从另一个文件输出结果的教程。

http://snag.gy/SI9lq.jpg

      <table border ='0'>
<form action="login.php" method="POST">
<tr><td>Username:</td> <td><input type="text" name="username" required></td></tr>
<tr><td>Password:</td> <td><input type="password" name="password" required></td><tr>
<tr><td><input button id="myBtn" type="submit" value="Log in"><td></tr>
<tr><td><a href='register.php'>Register</a></td></tr>
</table>




<?php

session_start();

$username = $_POST['username'];
$password = $_POST['password'];
$errors = array();


if ($username&&$password)
{

$connect = mysql_connect("localhost","root","") or die ("Could not connect");
mysql_select_db ("login") or die ("Could not find database");

$query = mysql_query("SELECT * FROM users WHERE username ='$username'");

$numrows = mysql_num_rows($query);
if ($numrows !=0)
{
while ($row =mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}

if ($username==$dbusername&&$password==$dbpassword)
{
echo header( 'Location: member.php' ) ;
$_SESSION['username']=$dbusername;
}
else
echo"
<script type=\"text/javascript\">
window.onload = function latestNews(){
var newPara = document.createElement('p');
var add_news = document.createTextNode('Incorrect password');
newPara.appendChild(add_news);
var getSecond = document.getElementById('footer');
document.body.insertBefore(newPara, getSecond);
};
</script>
";
}
else
die("That user dosen't exist");

}

else
die("Please enter a username and a password");
?>

编辑

我尝试添加 JavaScript,尽管它具有相同的效果。

最佳答案

要显示简单的错误消息,您只需在表单上方回显即可。看看这是否有效

<?php

session_start();

$username = $_POST['username'];
$password = $_POST['password'];
$errors = array();
$msg = ""; //define an error msg variable


if (isset($_POST['username'], $_POST['password'])) {

$connect = mysql_connect("localhost", "root", "") or die("Could not connect");
mysql_select_db("login") or die("Could not find database");

$query = mysql_query("SELECT * FROM users WHERE username ='$username'");

$numrows = mysql_num_rows($query);
if ($numrows != 0) {
while ($row = mysql_fetch_assoc($query)) {
$dbusername = $row['username'];
$dbpassword = $row['password'];
}

if ($username == $dbusername && $password == $dbpassword) {
echo header('Location: member.php');
$_SESSION['username'] = $dbusername;
$_SESSION['userid'] = $userid;
} else
$msg = "Incorrect password";
} else
$msg = "That user dosen't exist";

}

else
$msg = "Please enter a username and a password";

?>


<div class='errorMsg'><?php echo $msg; ?></div>
<form action="login.php" method="post"></form>
<table border='0'>
<tr>
<td>Username:</td>
<td><input name="username" required="" type="text"></td>
</tr>

<tr>
<td>Password:</td>
<td><input name="password" required="" type="password"></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><input id="myBtn" type="submit" value="Log in"></td>
<td></td>
</tr>
<tr>
<td>
<a href='register.php'>Register</a>
</td>
</tr>
</table>
</form>

关于javascript - PHP 验证表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27334690/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com