的價值,這是在PHP代碼無法從JSON
<?php session_start();
//Connect to database from here
$link = mysql_connect('****', '****', '****');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
//select the database | Change the name of database from here
mysql_select_db('****');
//get the posted values
$user_name=htmlspecialchars($_GET['user_name'],ENT_QUOTES);
$pass=$_GET['password'];
//now validating the username and password
$sql="SELECT user_name, password FROM tbl_user WHERE user_name='".$user_name."'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
//if username exists
if(mysql_num_rows($result)>0)
{
//compare the password
if(strcmp($row['password'],$pass)==0)
{
// Return success message
$message = array("flag" => "1", "msg" => "Login successfully.");
echo json_encode($message);
//Regenerate session ID to prevent session fixation attacks
//session_regenerate_id();
//now set the session from here if needed
//$_SESSION['u_name']=$user_name;
//$member=mysql_fetch_assoc($result);
//$_SESSION['u_id']=$member['id'];
//$name_show=$member['first_name'].' '.$member['last_name'];
//$_SESSION['name']=$name_show;
//Write session to disc
//session_write_close();
}
else
// Return error message
$message = array("flag" => "0", "msg" => "Incorrect password.");
echo json_encode($message);
}
else //if username not exists
{ // Return error message
$message = array("flag" => "0", "msg" => "Incorrect id.");
echo json_encode($message);
}
mysql_close($link);
?>
這是HTML代碼
$.ajax({
type: "get",
url: "http://mydomain.com/ajax_login.php",
data: {
user_name: $("#username").val(),
password: $("#password").val()
},
success: function(jsondata){
if (jsondata.flag == "0"){
//if flag is 0, indicate login fail
}
else {
//if flag is 1, indicate login success and redirect to another page
window.location.href = 'ABC.html';
}
},
datatype: "json"
});
顯示屏顯示"{\"flag\":\"0\",\"msg\":\"Incorrect id.\"}"
我的問題是,現在連標誌是0,它仍然會轉到ABC.html 如果要修改if子句,那麼如果標誌爲0,它仍然會在子句的真實部分?
EDITED這是編碼
你期待這段代碼做什麼?你正在創建一個'$ message'數組,然後'json_encode'然後'echo'出來。這正是你得到的。 – Steve 2013-03-13 22:08:37
請務必在jQuery ajax方法中用'dataType:「json」'配置您的ajax調用,並確保在您的PHP響應中發送JSON頭。 – MatRt 2013-03-13 22:09:55
這似乎對我很好。你想要它做什麼? – Dave 2013-03-13 22:10:29