这是我用于检查用户名是否存在的php代码。
$link = mysql_connect($server,$user,$pass);
mysql_select_db($db);
$response = 'no';
if(isset($_POST['username']) && trim($_POST['username']) != ''){
$query = mysql_query("SELECT * FROM prochatrooms_users WHERE username = '".mysql_escape_string(trim($_POST['username']))."'", $link);
if(mysql_num_rows($query) > 0){
$response = 'yes';
}
}
echo json_encode(array('exists' => $response));
这是javascript
$.customPOST = function(data,callback){
$.post('templates/default/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");
}
if(r.exists == 'yes'){
$('#check').attr('checked', false);
}
else{
$('#check').attr('checked', true);
}
});
}
此代码检查用户是否在用户名表密码字段中打开 现在我想检查密码表是否为空密码字段永远不会打开 这也是我的html表格
<form id="login" action="index.php" method="post" name="doLogin" onsubmit="return loginguest();">
<input type="hidden" name="login" value="1">
<input id="check" checked="checked" type="checkbox" name="isGuest" value="1" >
<div>نام خود را در کادر زير وارد کنيد:</div>
<input name="userName" id="username" value="فقط حروف انگليسي " type="text" autocomplete="off" maxlength="12" onkeypress="return restrict(event)" onkeyup="checkUsername();" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;">
<div id="lay_pw" style="display:none">
<div>رمز:</div>
<input name="userPass" id="password" value="" type="password" autocomplete="off">
</div>
答案 0 :(得分:0)
$password = $_POST['password'];
if($password == "")
{
echo 'Password should not be empty';
die();
}
另一种方式是
if(empty($password))
{
echo 'Password field should not be empty';
die();
}
答案 1 :(得分:0)
只需在loginguest函数中添加验证。
funciont loginguest (){
if(document.getElementById('username').value().length === 0){
alert('Username cannot be empty');
return false;
}
if(document.getElementById('password').value().length === 0){
alert('password cannot be empty');
return false; // to prevent submitting
}
// ... whatever is in the function
return true; // to submit form
}
答案 2 :(得分:0)
试试这个(未经测试):
$link = mysql_connect($server,$user,$pass);
mysql_select_db($db);
$response = 'no';
$response_pass = 'no'; //flag for password field
if(isset($_POST['username']) && trim($_POST['username']) != ''){
$query = mysql_query("SELECT * FROM prochatrooms_users WHERE username = '".mysql_escape_string(trim($_POST['username']))."'", $link);
if(mysql_num_rows($query) > 0){
$response = 'yes';
$row = mysql_fetch_array($query);
//checking if password exists for the username. Replace 'password' with the name of the password column from your table
$response_pass = ($row['password']!=NULL || $row['password']!='')? 'yes' : 'no' ;
}
}
echo json_encode(array('exists' => $response, 'pass_exist'=>$response_pass));
在你的JavaScript中:
function checkPassword() {
$.customPOST({username: $('#username').val()},function(r){
//password exists
if(r.pass_exist == 'yes'){
// the table contains a password value for this username so do what you need to here
}
else{
// the table DOES NOT contain a password value for this username
}
});
}