HUNG
HUNG

Reputation: 535

Cannot get the value from JSON

this is the code in php

    <?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);     
?>

this is the code in 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"
            });

display shows "{\"flag\":\"0\",\"msg\":\"Incorrect id.\"}"

my question is that, now even the flag is 0, it still goes to ABC.html what should the if clause to be modified such that if the flag is 0, it will still in the true part of the clause??

EDITED this is the more detail of the coding

Upvotes: 2

Views: 218

Answers (2)

MatRt
MatRt

Reputation: 3534

Be sure to configure your ajax call with dataType: "json" in you jQuery ajax method, and be sure to send JSON header in your PHP response like.

header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
header("Content-type: application/json");
echo json_encode($yourArray);

If you don't onfigure your ajax call correctly, the result could be interpreted as a simple string.

Upvotes: 1

Bogdan Burym
Bogdan Burym

Reputation: 5512

Perhaps jsondata.flag is undefined.
You need to decode response string to use it as JS object.
Or set dataType : 'json'...

Upvotes: 1

Related Questions