I wonder why it is not working?

check_login.php

<?php
session_start();
$data = array("username" => "true");
echo json_encode($data);
?>

my js file var linkName;

$.ajax({
    type: "POST",
    url: "check_login.php",
    dataType: "json",
    success: function(json){
    if(json.username != "true")
    {
      //do something
    }
    }
});

I am trying to get the username after checking whether or not the user has signed in yet in the php file, something like passing a session variable. But currently passing a string seems to already have a problem. Any know what I did wrong here?

Still not working the code above. Anyone want to help me out here?

share|improve this question

feedback

4 Answers

You have an error in your PHP code. The square bracket ] is not needed in line 3. And, of course you should use echo json_encode($data);

share|improve this answer
Still doesn't work – Ryan Fung 14 hours ago
You should check the response of the server with a tool like Firebug for Firefox or Developer Tools in Google Chrome. That way you can see if the server outputs anything or not. – markz 14 hours ago
feedback
    `if(json.data.username != "true")` is wrong you do not have data object

    `if(json.username != "true")` try this
share|improve this answer
feedback

You should echo the json data so that the page you get using ajax contains the string data.

<?php
session_start();
$data = array("username" => "true"]);
echo(json_encode($data));
?>

$.ajax({
    type: "POST",
    url: "check_login.php",
    dataType: "json",
    success: function(json){
       console.log(json)
    }
});

Also some times you might need to set the content type of the header in php to json.

share|improve this answer
1  
Open the console in chrome or mozilla to view the console.log this is usualy done by clicking right any where in the webbrowser and then selecting inspect element. after that you will find a tab called console where logs appear. – Pablo Karlsson 15 hours ago
accept as answer if it worked out thanks. :D – Pablo Karlsson 14 hours ago
still don't know why it doesn't work – Ryan Fung 14 hours ago
Have you tried surfing in to check_login.php and looking at that page what dose that show??? – Pablo Karlsson 13 hours ago
Any good way to debug php? – Ryan Fung 4 hours ago
feedback
 return json_encode($data);

I think you have to return the data rather than just json encoding it!

share|improve this answer
still not working – Ryan Fung 14 hours ago
it's echo json_encode($data), not return – markz 14 hours ago
return will not echo the output – nerdklers 14 hours ago
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.