我试图让我的代码在未填写用户名字段和密码字段时显示错误消息。当我使用 If(isset) 时它不起作用,但是当我使用 if (普通代码) 时它似乎起作用。任何帮助将不胜感激。
这是代码:
登录.php
<?php
if (isset($_POST['log_username']) && isset($_POST['log_password'])){
$log_uname = preg_replace('#[^A-za-z0-9]', '', $_POST["log_username"]);
$log_password = preg_replace('#[^A-za-z0-9]#i', '', $_POST["log_password"]);
//get user from database
$sql = mysql_query("SELECT * FROM users WHERE username='$log_uname' AND password='$log_password' LIMIT 1");
//check user existance
$userCount = mysql_num_rows($sql);
if ($userCount == 1) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
}
$_SESSION['id'] = $id;
$_SESSION['log_username'] = $log_uname;
$_SESSION['log_password'] = $log_password;
}else{
die (msg1(0, "Information Incorrect"));
}
}
function msg1($status1,$txt1){
return '{"status1":'.$status1.',"txt1":"'.$txt1.'"}';
}
?>
登录.js
$(document).ready(function (){
$('.login_form').submit(function(e) {
login();
e.preventDefault();
});
function login(){
hideshow1('loading1',1);
error1(0);
$.ajax({
type: "POST",
url: "php/login.php",
data: $('.login_form').serialize(),
dataType: "json",
success: function(msg1){
if(parseInt(msg1.status1)==1){
window.location=msg1.txt1;
}
else if (parseInt(msg1.status1)==0){
error1(1,msg1.txt1);
}
hideshow1('loading1',0)
}
});
}
function hideshow1(el,act){
if(act) $('#'+el).css('visibility','visible');
else $('#'+el).css('visibility','hidden');
}
function error1(act,txt1){
hideshow1('error1',act);
if(txt1) $('#error1').html(txt1);
}
});
最后一个 Index.php
<?php include ("php/head.php");?>
<div id="wrapper">
<div id="container">
<div id="intro">
<h2><center>Are you a member? Login ...</center></h2>
<h3><center>And enjoy hundreds of services.</center></h3>
<div id="log_form">
<form action="php/login.php" method="POST" class="login_form">
<input type="text" size="25" name="log_username" id="login_txt" placeholder="Your Username">
<input type="password" size="25" name="log_password" id="login_pass" placeholder="Your Password">
<label id="check"><input type="checkbox" name="checkbox"> Remember Me</label>
<input type="submit" name="submit1" id="login_sub" value="LogIn">
<img id="loading1" src="images/ajax-loader.gif" alt="working.." />
<script>
$("#login_txt,#login_pass").click(function (e) {
e.preventDefault();
$('#error1').fadeOut('fast', function () {
$(this).show();
$(this).css("visibility","hidden");
});
});
</script>
</form>
<div id="error1"></div>
</div>
<div id="new_users">
<h4 class="h4_users">New Users ...</h4>
<a href="#"><img src="#" width="55" height="55"></a>
<a href="#"><img src="#" width="55" height="55"></a>
<a href="#"><img src="#" width="55" height="55"></a>
<a href="#"><img src="#" width="55" height="55"></a>
</div>
</div>
<div id="register">
<h2><center>Sign up Below ...</center></h2>
<h3><center>Easy, fast and free!</center></h3>
<form action="php/register.php" method="POST" name="form" class="form">
<input type="text" size="25" name="fname" placeholder="First Name" id="fname" >
<input type="text" size="25" name="lname" placeholder="Last Name">
<input type="text" size="25" name="username" placeholder="Username">
<input type="text" size="25" name="email" placeholder="Email">
<input type="text" size="25" name="email2" placeholder="Repeat Email">
<input type="password" size="25" name="password" placeholder="Password">
<input type="password" size="25" name="password2" placeholder="Repeat Password">
<input type="submit" name="submit" id="sub" value="Sign Up!">
<img id="loading" src="images/ajax-loader.gif" alt="working.." />
</form>
<script>
$('#fname').click(function (e) {
e.preventDefault();
$('#error').fadeOut('fast', function () {
$(this).show();
$(this).css("visibility","hidden");
});
});
</script>
<div id="error"></div>
</div>
</div>
<?php include("php/footer.php");?>
</div>
</body>
</html>