[英]How to validate the username and password and also the selected role from database using javascript/ajax or any other validation formats
I just want to indicate user your name is wrong ur password is wrong ur role is wrong ., using any validations like ajax/javascript or any other.. 我只想指示用户您的名称错误,密码错误,角色错误,请使用ajax / javascript或其他任何验证。
index.php 的index.php
<html>
<head>
</head>
<body>
<style>
body {
width:400px;
height:auto;
margin: 0 auto;
padding-top:20px;
}
body a {
text-decoration:none;
color:#0099CC;
}
.border{
border:#999999;
border-style:solid;
border-width:thin;
}
</style>
<?php
include('connect.php');
?>
<table width="350" class="border" >
<form id="form1" name="form1" method="post" action="login.php" onSubmit="return validate()">
<tr>
<td width="112"> </td>
<td colspan="2"> <strong>Login</strong></td>
<td width="22"> </td>
</tr>
<tr>
<td height="35">
<div align="right">User Name : </div>
</td>
<td colspan="2"><label>
<input type="text" name="name" id="name" /></label>
</td>
<td> </td>
</tr>
<tr>
<td>
<div align="right">Password : </div>
</td>
<td colspan="2"><input type="password" name="password" id="password" /></td>
<td> </td>
</tr>
<tr>
<td>
<div align="right">select your role : </div>
</td>
<td colspan="2">
<!-- <input type="radio" checked="checked" name="title" value="member" id="member" /> member</label>
<label for="admin"> <input type="radio" name="title" value="admin" id="admin" /> admin</label> -->
<label>
<select name="member" id="member">
<option title="title" value="member"> member
<option title="title" value="admin"> admin
</select>
</label>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td width="69"><label>
<input type="submit" name="Submit" value="Login" id="add" /></label>
</td>
<td width="127"><label>
<input type="reset" name="reset" value="reset" /></label>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><a href="register.php">Register ?</a> </td>
<td><a href="forget_password.php">Forget Password?</a> </td>
<td> </td>
</tr>
</form>
</table>
<script>
function validate()
{
if( document.form1.name.value == "" )
{
alert( "Please provide your name!" );
document.form1.name.focus() ;
return false;
}
if( document.form1.password.value == "" )
{
alert( "Please provide your password!" );
document.form1.password.focus() ;
return false;
}
}
</script>
</body>
</html>
login.php 的login.php
<?php
session_start();
include('connect.php');
$name = $_POST['name'];
$password = $_POST['password'];
$role = $_POST['member'];
$sql = "select * from login_register where name = '$name'
and password = '$password' and role = '$role'";
$retain = mysql_query($sql, $con);
// Mysql_num_row is counting table row
$count=mysql_num_rows($retain);
$row=mysql_fetch_array($retain);
if($count == 1)
{
if ( $role == "admin")
{
$_SESSION['adminuser']=$_POST['name'];
header("location:admin_home.php");
}
else if ( $role == "member")
{
header("location:member_home.php");
$_SESSION['user']=$_POST['name'];
}
}
else
{
echo "<a href='index.php'>click here to login </a>";
}
?>
I just want to indicate user your name is wrong ur password is wrong ur role is wrong ., using any validations like ajax/javascript or any other. 我只想表明用户名,您的密码错误,您的角色错误。,请使用ajax / javascript或其他任何验证方法。
Here is a sample jquery code. 这是一个示例jquery代码。 May be it'll help you. 可能会对您有所帮助。 It expect the response data in JSON format & then according to the response data value do the rest of execution. 它期望响应数据为JSON格式,然后根据响应数据值执行其余的操作。
$.ajax({
type : "POST",
data : { username: "user name", password: "user password", role:"your role" },
url : 'some.php',
dataType: 'json',
success : function(response) {
console.log( "response data: " + response );
}
});
For more : http://api.jquery.com/jquery.ajax/ 有关更多信息: http : //api.jquery.com/jquery.ajax/
<?php
session_start();
include('connect.php');
if(isset($_POST['name']))
{
$sql = "select * from login_register where name = ".$_POST['name'];
$retain = mysql_query($sql, $con);
// Mysql_num_row is counting table row
$count=mysql_num_rows($retain);
if($count < 1)
{
echo "Wrong user name";
}
}
?>
like this copy and paste for all password and roll 像这样复制并粘贴所有密码并粘贴
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.