嗨,我有两个文本框。一个用于用户名,另一个用于密码。目前,如果任何字段留空,我有显示错误消息的代码“凭据无效”。这会将我带到显示错误消息的新页面,然后返回主页,我可以在其中再次输入我的用户名和密码。
我希望在文本框本身附近完成验证部分。例如,如果用户名文本框留空并单击提交按钮,它应该显示一条消息,例如请在文本框本身附近输入用户名。
HTML 代码:
<form method="post" action="login.php">
<p class="user_text">USER LOGIN</p>
<p class="user_name_text">username<span style="color:#f60219;"> *</span></p>
<p style="padding:8px 0 0 5px;">
<input type="text" name="username" minlength="2" maxlength="20" class="contact_filed" value="enter your name" onfocus="javascript:clearField(this,'enter your name')" onblur="javacript:fillField(this,'enter your name')" /></label>
</p>
<p class="user_name_text1">password<span style="color:#f60219;"> *</span></p>
<p style="padding:8px 0 0 5px;">
<input type="password" name="password" maxlength="20" class="contact_filed" value="password" onfocus="javascript:clearField(this,'password')" onblur="javacript:fillField(this,'password')" />
</p>
<p style="padding:16px 0 0 16px;"><input name="submit" type="submit" value="Submit" class="log" /></p>
<p style="padding:12px 0 0 16px;"><a href="#" class="read_more1">Create new account</a><a href="#" class="read_more1">Request new password</a></p>
</form>
用于验证的 PHP 代码:
if ($_POST['submit']) {
include('connect.php');
$u = mysql_real_escape_string($_POST['username']);
$p = md5($_POST['password']);
$q = mysql_query("SELECT id FROM $login_db_table WHERE username='$u' AND password='$p' LIMIT 1");
if (mysql_fetch_array($q, MYSQL_ASSOC)) {
if ($_POST['remember']) {
setcookie('username', $_POST['username'], time() + 5000000);
setcookie('password', md5($_POST['password']), time() + 5000000);
} else {
setcookie('username', $_POST['username']);
setcookie('password', md5($_POST['password']));
}
if($u != "" && $p !=""){
echo '<p>Login successful.</p>';
header("Location: main.php");
}
}
else
echo "Login failed. Please enter valid credentials.";
echo "<script>setTimeout(\"location.href = 'http://localhost/CashTree/reg/Home/index.html';\",1500);</script>";
//echo '<p>Login failed. Please enter your correct credentials</p>';
}