这是我的html表单。
<form id="login" action="">
<input name="username" id="username" value="" maxlength="100" type="text" />
<div>username:</div>
<div id="lay_pw" style="display:none">
<input name="password" id="password" value="" type="password" />
<div>password:</div>
</div>
<input type="submit" name="go" value="login" class="main_btn1" />
</form>
如果用户名不存在,我想检查mysql数据库中是否已存在用户名显示隐藏密码字段传输用户例如index.php。我想使用ajax检查
答案 0 :(得分:0)
1)在根目录中创建一个新文件夹。将3个以下文件放入其中:
2)创建一个HTML文件名“ index.php ”,然后将此代码复制/粘贴并保存。
<!DOCTYPE html>
<html lang="fr_FR">
<head>
<meta charset="utf-8">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="check_username.js"></script>
<title>Username check</title>
</head>
<body>
<form id="login" action="" method="post">
<div>username:</div>
<input name="username" id="username" value="" maxlength="100" type="text" onkeyup="checkUsername();" />
<div id="lay_pw" style="display:none">
<div>password:</div>
<input name="password" id="password" value="" type="password" />
</div>
<br />
<input type="submit" name="go" value="login" class="main_btn1" />
</form>
</body>
</html>
3)创建一个javascript文件并将其命名为“ check_username.js ”,然后将其中的代码复制/粘贴并保存。
$.customPOST = function(data,callback){
$.post('check_username.php',data,callback,'json');
}
//when typing, the script checks if the username exists
function checkUsername() {
$.customPOST({username: $('#username').val()},function(r){
//username exists
if(r.exists == 'yes'){
$( "#lay_pw" ).toggle(400,function(){});
}
else{
$( "#lay_pw" ).css("display", "none");
}
});
}
4)创建一个PHP文件,将其命名为“ check_username.php ”,将此代码复制/粘贴并保存。
<?php
$link = mysql_connect('localhost','root','');
mysql_select_db('database');
$response = 'no';
if(isset($_POST['username']) && trim($_POST['username']) != ''){
$query = mysql_query("SELECT * FROM table WHERE username = '".mysql_escape_string(trim($_POST['username']))."'", $link);
if(mysql_num_rows($query) > 0){
$response = 'yes';
}
}
echo json_encode(array('exists' => $response));
?>
5)使用此示例数据库(我用它来测试这些东西)
CREATE TABLE IF NOT EXISTS `users` (
`user` varchar(50) NOT NULL,
`password` varchar(32) NOT NULL,
PRIMARY KEY (`user`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `users` (`user`, `password`) VALUES
('test1', '5a105e8b9d40e1329780d62ea2265d8a'),
('test2', 'ad0234829205b9033196ba818f7a872b'),
('alpha', '2c1743a391305fbf367df8e4f069f9f9'),
('beta', '987bcab01b929eb2c07877b224215c92');
答案 1 :(得分:0)
这是我的check.php文件
<?php
session_start();
include_once('inc/dbConnect.inc.php');
$message=array();
if(isset($_POST['username']) && !empty($_POST['username'])){
$username=mysql_real_escape_string($_POST['username']);
}else{
$message[]='Please enter username';
}
if(isset($_POST['password']) && !empty($_POST['password'])){
$password=mysql_real_escape_string($_POST['password']);
}else{
$message[]='Please enter password';
}
$countError=count($message);
if($countError > 0){
for($i=0;$i<$countError;$i++){
echo ucwords($message[$i]).'<br/><br/>';
}
}else{
$query="select * from user where username='$username' and password='$password'";
$res=mysql_query($query);
$checkUser=mysql_num_rows($res);
if($checkUser > 0){
$_SESSION['LOGIN_STATUS']=true;
$_SESSION['username']=$username;
echo 'correct';
}else{
echo ucwords('please enter correct user details');
}
}
?>