我在寫一個jQuery的登錄。如果php登錄腳本(checklogin.php)迴應「true」,那麼我的jquery頁面應該會發出警報。當我運行一個login.php(不使用jquery)時,它將請求發送到checklogin.php,它反映了「true」,但是如果我使用我的jquery登錄腳本將登錄信息發送到checklogin.php,則警報I跟蹤回說「假」。json_encode布爾返回錯誤的值
$('#login').click(function(e){
$.getJSON("http://www.hedonsoft.com/game/login.php",{username: $("#username").val(), password: $("#password").val()},function(data){
if(data==true){
alert(data.boolean);
//$('#logindiv').slideUp();
//$('#gamediv').show();
//$('#registerdiv').hide();
//$('#gameheader').html(data['username']);
}else{
alert("false");
}
});
});
<?php
header('Access-Control-Allow-Origin: *');
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="hedonsof_conflict"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
$json = array('boolean' => 'true', 'jsonUser' => $myusername);
echo "true";
}
else {
echo "Wrong Username or Password";
}
?>
所以長話短說,當我嘗試從一個普通的PHP頁面的價值我跟蹤是真的,當我試圖從一個jQuery頁面的價值我跡是假的登錄到登錄;
編輯:
<?php
...
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
$json = array('boolean' => true, 'jsonUser' => $myusername);
echo true;
}
else {
echo "Wrong Username or Password";
}
?>
而我的jQuery ...
$('#login').click(function(e){
$.getJSON("http://www.hedonsoft.com/game/login.php",{username: $("#username").val(), password: $("#password").val()},function(data){
if(data){
alert("true");
//$('#logindiv').slideUp();
//$('#gamediv').show();
//$('#registerdiv').hide();
//$('#gameheader').html(data['username']);
}else{
alert(data); // shows null
}
});
});
隨着json_encode
,布爾的值返回null
$('#login').click(function(e){
$.getJSON("http://www.hedonsoft.com/game/login.php",{username: $("#username").val(), password: $("#password").val()},function(data){
if(data.boolean == "true"){
alert("true");
//$('#logindiv').slideUp();
//$('#gamediv').show();
//$('#registerdiv').hide();
//$('#gameheader').html(data['username']);
}else{ alert(data);
}
}); });
<?php
...
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
$json = array('boolean' => true, 'jsonUser' => $myusername);
echo json_encode($json);
}
...
?>
json_encode實際上是用在某個地方,還是隻是收集回聲「真」;作爲ajax響應? – Mahn 2012-07-24 21:13:00
請參閱我的編輯。這是我最初試圖做的,但是無論我做了什麼,它都保持返回null,所以我想要嘗試一個簡單的字符串或布爾檢查會更容易,一旦我得到那個工作,然後我會再試一次。 – RapsFan1981 2012-07-24 21:44:36
@ RapsFan1981:查看更新的答案。 – Ryan 2012-07-24 21:45:29