首先你的验证有简单了Null验证,这些东西直言在js里写几个判断就搞定了。
用户名已经存在的验证设计到数据库,所以,你要去看看js高级篇了。看看,简单的ajax验证,你就知道了。呵呵!
没有给代码!但我相信你用点心,能搞出来的。呵呵!加油!
第一次学PHP就是做这个验证..
html做个表单,
当表单onsubmit=return check()调用自写js来判断用户名和密码是否为空,
如果是空就alert不能为空,然后return false相反则return true
而接收的PHP也要验证是否为空,如果严谨点还要对提交的数据进行过滤,防止sql注入。
然后php再根据提交的数据搜MYSQL,如果用户名和密码都相同时,echo 登录成功,相反则登录失败.
<html><script>
function check(obj){
with(obj){
if((user.value+"").length <= 0){
alert("用户名不能为空")
return false
}else if((pwd.value+"").length <= 0){
alert("用户名不能为空")
return false
}else{
return true
}
}
}
</script>
<body>
<form action="check.php" method="post" onsubmit="return check(this)">
<input type="text" name="user" value="">
<input type="password" name="pwd" value="">
<input type="submit" name="submit" value="登录">
<input type="cancel" name="cancel" value="取消">
</form>
</body>
</html> <?php
$conn = mysql_connect( "数据库地址", "数据库用户名", "密码" )
mysql_query("set names utf8")
mysql_select_db( "数据库名" )
function inject_check($sql_str){
return preg_match("/select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile|%|eval|=|and|'||exec|count/i", $sql_str) // 进行过滤
}
if(!empty($_POST)){
foreach($_POST as $key => $value){
if(inject_check($value)){
exit ('<script>alert("地址栏输入发现有非法字符,请重新输入!")history.go(-1)</script>')
die ()
}
}
}
$res = mysql_query("SELECT count(*) as m from `表名` where 用户名='${_POST['user']}' AND 密码='${_POST['pwd']}'")
$row = mysql_fetch_object($res)
if($row->m >0){
echo "登陆成功"
}else{
echo "用户名或密码错误"
}
exit
?>
一般js使用ajax技术,要知道添加数据成功这一动作,需要后台返回信息可以使用jquery提供的ajax方法
$.ajax({
url: "test.jsp",
dataType: 'json',
data: "name="+$("#name").val()
success: function(jsonDate){
if(jsonDate!=null){
alert("成功")
}
}
})
望采纳
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)