Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

4 Answers

up vote 4 down vote accepted

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 Sep 22 '12 at 7:28
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 Sep 22 '12 at 7:30
    `if(json.data.username != "true")` is wrong you do not have data object

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

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 Sep 22 '12 at 6:24
accept as answer if it worked out thanks. :D – Pablo Karlsson Sep 22 '12 at 7:23
still don't know why it doesn't work – Ryan Fung Sep 22 '12 at 7:39
Have you tried surfing in to check_login.php and looking at that page what dose that show??? – Pablo Karlsson Sep 22 '12 at 8:40
Any good way to debug php? – Ryan Fung Sep 22 '12 at 17:28
show 1 more comment
 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 Sep 22 '12 at 7:23
it's echo json_encode($data), not return – markz Sep 22 '12 at 7:27
return will not echo the output – Niklas Modess Sep 22 '12 at 7:28

Your Answer

 
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.