I would like to parse through jquery. When I want to parse, there is an error - undefined.

This is my code:

PHP

try { 

session_start();

require '../SqlConfig.php';


 if(isset($_POST["user_name"]) && isset($_POST["user_password"])) {
 {  

$sql = "SELECT * FROM users WHERE user_name='".mysql_real_escape_string($_POST["user_name"]) . "' AND " . "user_password='". mysql_real_escape_string($_POST["user_password"]) ."'";

$result = mysql_query($sql) or die(mysql_error());

$jsonresult = array();

  while($row = mysql_fetch_array($result))
  {
    $thisResult = array();

    $thisResult["user_auth"] = 1;
    $thisResult["user_id"] = $row['user_id'];
    $thisResult["user_name"] = $row['user_name'];

    $_SESSION["user_auth"] = 1;
    $_SESSION["user_id"] = $row['user_id'];
    $_SESSION["user_name"] = $row['user_name'];

    $jsonresult[] = $thisResult;
  }
  echo json_encode($jsonresult);
  mysql_close();
  }

}
}
catch(Exception $e) 
{
echo $e->getMessage();
}

JSON ARRAY:

[{"user_auth":1,"user_id":"11","user_name":"Jenan"},
 {"user_auth":1,"user_id":"15","user_name":"Jenan2"},
 {"user_auth":1,"user_id":"16","user_name":"Jenan3"}]

JQUERY

getAuthentification: function(username, password)
    {
        $.post('ActionScripts/Authentification.php',{
            user_name: username, 
            user_password: password
        }, function(data) 
        {
          $.each(data, function(key, value){
                alert(value.user_auth +' - '+ value.user_id +' - '+ value.user_name);
            });


        });
    }

Here is the error: The result is undefined - undefined - undefined. And an endless cycle. -

$.each(data, function(key, value){
                alert(value.user_auth +' - '+ value.user_id +' - '+ value.user_name);
            });

data -

 [{"user_auth":1,"user_id":"11","user_name":"Jenan"},
 {"user_auth":1,"user_id":"15","user_name":"Jenan2"},
 {"user_auth":1,"user_id":"16","user_name":"Jenan3"}]

alert - output - undefined - undefined - undefined

Thanks for the advice.

share|improve this question
1  
Debug it by doing a console.log(data) right before your $.each. – Xeon06 Sep 19 '11 at 18:54
Data - [{"user_auth":1,"user_id":"11","user_name":"Jenan"}, {"user_auth":1,"user_id":"15","user_name":"Jenan2"}, {"user_auth":1,"user_id":"16","user_name":"Jenan3"}] in console.log(data). I tried it. – Jenan Sep 19 '11 at 18:56
feedback

1 Answer

up vote 4 down vote accepted

try this:

getAuthentification: function(username, password)
    {
        $.post('ActionScripts/Authentification.php',{
            user_name: username, 
            user_password: password
        }, function(data) 
        {
          $.each(data, function(key, value){
                alert(value.user_auth +' - '+ value.user_id +' - '+ value.user_name);
            });


        },"json");
    }

all i added was ,"json" to the end to tell jquery that it should expect a json response instead of a string response.

share|improve this answer
Very good Tentonaxe! Thank you! – Jenan Sep 19 '11 at 19:01
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.