我是服务器验证的新手,我在网上找遍了,找不到东西。我试图通过张贴它自己和返回1或0在数组中根据表单是否有效php验证表单。这很好,但我的问题是,我想让登录屏幕淡出,如果结果是1。我不能让它工作。
下面是我的代码:
PHPif ($valid == 1) {
$isvalid = array('valid' => $valid);
echo json_encode(array_values($isvalid));
};
javascript <script>
$(document).ready(function(){
$('#login_button').click(function(){
$.getJSON("/index.php",function(result){
console.log(result);
});
});
});
</script>
nothing输出到控制台,我也尝试了alert,没有发生任何事情。JSON似乎在左上角工作,我得到一个[1],这应该是我需要的。
这是我的整个代码也许这是放置脚本的问题?
<script type="text/javascript" src="/jquery-1.9.1.min.js">
</script>
''wait to load the script until the document is ready
<script>
$(document).ready(function(){
//on submission
$('#login_button').click(function(){
//check if login was valid
$.getJSON("localhost/index.php",function(result){
console.log(result) .error(function() {console.log('error');});
});
});
});
</script>
<?PHP
$valid = 0;
//Declare the database connection
$con = mysql_connect("localhost","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//Has the form been submitted?
if(isset($_POST['Username'])){
//Check if Username and PW correct
mysql_select_db ("Users",$con);
$sqlCheckUser = mysql_query ( "select Username from userinfo where UserName = '$_POST[Username]' && Password = MD5('$_POST[Password]')");
//Parse Results
$row = mysql_fetch_assoc($sqlCheckUser);
//is the login valid
if (isset($row) && !empty ($row)) {
$valid = 1;
}
else {
$badpw = "Invalid Username or Password";
}
}
//handle if form has not been submitted
else {
echo "<center>Please Enter Username and Password</center><br />";
};
if ($valid == 1) {
$isvalid = array('valid' => $valid);
echo json_encode(array_values($isvalid));
};
?>
</head>
我验证了我的JSON,它看起来不错,有人知道发生了什么吗?我真的需要这件事成功。
编辑所以把这个附加到我的代码中确实会出现错误
脚本现在看起来像下面的
<script>
$(document).ready(function(){
$('#login_button').click(function(){
$.getJSON("index.php",$('#Login').serialize(),function(result){
alert(result);
})
.error(function(error) { alert(error); console.log(error); })
})
});
</script>
警告文本为[object object]
控制台输出如下:
object ready state: 0
getResponseHeader: Function
getAllRequestHeaders: Function
SetRequestHeader: Function
overrideimetype: function
https://i.stack.imgur.com/roQ6J.jpg(我截屏了,因为它在控制台上停留的时间不够长,我看不清)
您的响应数据是在result
变量在您的回调函数,所以更改:
console.log(json);
console.log(result);