如何在登录表单中添加登录表单验证错误,错误消息不会出现在loginform中
<form action="test.php" method="POST" >
<div class="logintable ">
<table class="applyfont" style="border: 1px solid lightseagreen">
<tbody>
<tr id="login1">
<td class="applyfontbold"><b>Login </b></td>
</tr>
<tr>
<td style="padding-right:10px;">
<input type="text" name="name" value="" placeholder="Username or Email" id="uname">
<span class="error">* <?php echo $nameErr;?></span>
</td>
</tr>
<tr>
<td>
<input type="password" name="password" value="" placeholder="Password" id="password">
<span class="error">* <?php echo $passeErr;?></span>
</td>
</tr>
<tr>
<td>
<p class="submit"><input type="submit" name="save" value="Submit"> </p>
</td>
</tr>
<tr>
<td>
<div class="login-help applyfontlighter">
<p>Forgot your password? <span id="footer"><u style="color:red;"><a href="#">Click here to reset it.</a></u></span></p>
</div>
<p id="new"><button type="button" class="btn btn-default btn-xs" >New Registers</button></p>
</td>
</tr>
</tbody>
</table>
</div>
</form>
<?php
$x = "typepass";
if (isset($_POST['save'])) {
if (empty($_POST['name'])) {
print "Name is required";
} else {
$name = $_POST['name'];
}
if (empty($_POST['password'])) {
print "<br> password is required";
} elseif ($_POST['password'] !== $x) {
print "password doesn't match";
} else {
$password = $_POST['password'];
}
print "Username :" . " " . $name . "<br>" . "Password :" . " " . $password;
}
?>
&#13;
中
答案 0 :(得分:1)
如果你不想像你说的那样想使用AJAX,那么这两个文件都必须是PHP才能工作。你需要使用SESSIONS。这样做(我删除了不必要的代码):
<强> page.php文件强>
<?php
session_start();
?>
<form action="submit.php" method="POST">
<input type="text" name="name" value="" placeholder="Username or Email" id="uname">
<?php if (!empty($_SESSION['name_error'])) { ?>
<span class="error">* <?= $_SESSION['name_error'] ?></span>
<?php } ?>
<input type="password" name="password" value="" placeholder="Password" id="password">
<?php if (!empty($_SESSION['pass_error'])) { ?>
<span class="error">* <?= $_SESSION['pass_error'] ?></span>
<?php } ?>
<input type="submit" name="save" value="Submit">
</form>
<?php
session_destroy();
?>
<强> submit.php 强>
<?php
session_start();
$x = "typepass";
if (isset($_POST['save'])) {
$errors = 0;
if (empty($_POST['name'])) {
$_SESSION['name_error'] = "Name is Required";
$errors++;
}
if (empty($_POST['password'])) {
$_SESSION['pass_error'] = "password is required";
$errors++;
} else if ($_POST['password'] !== $x) {
$_SESSION['pass_error'] = "password doesn't match";
$errors++;
}
if ($errors > 0) {
//go back to prev page
header("Location:" . $_SERVER['HTTP_REFERER']);
} else {
//do your logic
}
}
?>
答案 1 :(得分:0)
你需要添加一个段落标记,并在收到jquery的错误后将错误信息添加到其中,你可以按照本教程
http://phppot.com/jquery/jquery-form-validation-with-tooltip/
答案 2 :(得分:0)
我不是说这段代码是标准格式。使用$ _SESSION可以通过调用session_start()来初始化会话,而不会调用你无法存储会话变量。
I like to create error message in after the under the input field
是CSS问题
那么你需要修改css,因为你想显示
<强> HTML 强>
<?php
session_start();
?>
<style type="text/css">
.error {
width : 100%; /* this is example giving*/
float : left; /*you can modify as you like*/
}
</style>
<form action="test.php" method="POST" >
<div class="logintable ">
<table class="applyfont" style="border: 1px solid lightseagreen">
<tbody>
<tr id="login1">
<td class="applyfontbold"><b>Login </b></td>
</tr>
<tr>
<td style="padding-right:10px;">
<input type="text" name="name" value="" placeholder="Username or Email" id="uname">
<span class="error">* <?php echo isset($_SESSION['nameErr'])?$_SESSION['nameErr']:'';?></span>
</td>
</tr>
<tr>
<td>
<input type="password" name="password" value="" placeholder="Password" id="password">
<span class="error">* <?php echo isset($_SESSION['passwordErr'])?$_SESSION['passwordErr']:'';?></span>
</td>
</tr>
<tr>
<td>
<p class="submit"><input type="submit" name="save" value="Submit"> </p>
</td>
</tr>
<tr>
<td>
<div class="login-help applyfontlighter">
<p>Forgot your password? <span id="footer"><u style="color:red;"><a href="#">Click here to reset it.</a></u></span></p>
</div>
<p id="new"><button type="button" class="btn btn-default btn-xs" >New Registers</button></p>
</td>
</tr>
</tbody>
</table>
</div>
</form>
<强> test.php的强>
<?php
session_start();
$x = "typepass";
if (isset($_POST['save'])) {
if (empty($_POST['name'])) {
$_SESSION['nameErr'] = "Name is required";
$status = false;
} else {
isset($_SESSION['nameErr']) ? session_unset($_SESSION['nameErr']):null;
$name = $_POST['name'];
$status = true;
}
if (empty($_POST['password'])) {
$_SESSION['passwordErr'] = " password is required";
$status = false;
} elseif ($_POST['password'] !== $x) {
$_SESSION['passwordErr'] = "password doesn't match";
$status = false;
} else {
isset($_SESSION['passwordErr'])? session_unset($_SESSION['passwordErr']):null;
$password = $_POST['password'];
$status = true;
}
if($status === false){
header("Location:login.php"); //rediret to login if error
}
print "Username :" . " " . $name . "<br>" . "Password :" . " " . $password;
}
?>