1

Environment: PHP 5.3.5 MySQL Server 5.5.8

Created my first ever ajax call and after much determination I have got results. The results appear to be returning as a string. for when I request

alert(result[0]);

I am only returned a single character.

If I try to use a field name, I have a value of undefined returned.

Here is the javascript for my AJAX:

$.ajax({ 
    type: "POST", 
    url: "models/ajaxHandler.php", 
    data: "handler=this&stu=21", 
    success: function(result){ 
            if (result !== null) 
            { 
                    alert(result + " " + result[0] +" " result['firstname']);  
                    var obj =  JSON && JSON.parse(result) || $.parseJSON(result);
                    alert(obj + " " + obj[0] + " " + obj['firstname']);
            } 
            ShowDialog(false); 
            e.preventDefault(); 
    }, 
    error: function()
    { 
            alert("ajax failure"); 
    } 
});

So once the result is captured using a PDO connection in PHP to mysql with a fetchAll(). I return the results from the php like so:

echo json_encode($results);

The alerts for the results comeback like so:

[{"firstname":" Test","0":" Test","lastname":" One","1":" One","id":"2","2":"2","st_usage_id":null,"3":null},{"firstname":" Mr","0":" Mr","lastname":" Two","1":" Two","id":"3","2":"3","st_usage_id":null,"3":null},{"firstname":" Mr","0":" Mr","lastname":" Three","1":" Three","id":"5","2":"5","st_usage_id":null,"3":null}] [ undefined

The parse of the JSON returns all results as object.

I have also tried returning the results in php using:

print_r(json_encode($results));

It displays the same string above. Lastly I used

$var_dump(results);

This returned the following:

array(3) {
  [0]=>
  array(8) {
    ["firstname"]=>
    string(7) " DrTest"
    [0]=>
    string(7) " DrTest"
    ["lastname"]=>
    string(4) " One"
    [1]=>
    string(4) " One"
    ["user_public_info_id"]=>
    string(1) "2"
    [2]=>
    string(1) "2"
    ["st_usage_id"]=>
    NULL
    [3]=>
    NULL
  }
  [1]=>
  array(8) {
    ["firstname"]=>
    string(3) " Dr"
    [0]=>
    string(3) " Dr"
    ["lastname"]=>
    string(4) " Two"
    [1]=>
    string(4) " Two"
    ["user_public_info_id"]=>
    string(1) "3"
    [2]=>
    string(1) "3"
    ["st_usage_id"]=>
    NULL
    [3]=>
    NULL
  }
  [2]=>
  array(8) {
    ["firstname"]=>
    string(3) " Dr"
    [0]=>
    string(3) " Dr"
    ["lastname"]=>
    string(6) " Three"
    [1]=>
    string(6) " Three"
    ["user_public_info_id"]=>
    string(1) "5"
    [2]=>
    string(1) "5"
    ["st_usage_id"]=>
    NULL
    [3]=>
    NULL
  }
}
 a undefined

I am not sure what it is I am missing, and I'm sure it is something simple. I feel like I have tried everything. Please could someone tell me where I went wrong?

5
  • I tried traverseing through the results usiong the following snippets too. The results always returned undefined (indefinitly) $.each(result, function (i, elem) { alert(elem.firstname); }); Commented Sep 11, 2013 at 19:52
  • 1
    alert(obj[0]['firstname']) Commented Sep 11, 2013 at 19:53
  • And this is the other attempt: result.foreach(function(entry){ alert(entry); }); Commented Sep 11, 2013 at 19:53
  • tried that too. the second alert does not work. Commented Sep 11, 2013 at 19:56
  • Thank you. That was indeed the issue. I was accessing the elements of the obj as a single array item. Commented Sep 12, 2013 at 12:04

4 Answers 4

4

Specify the json datatype.

dataType: "json"

Note, the result will never be null in recent versions of jQuery, instead it'll go to error in the event nothing gets returned.

Sign up to request clarification or add additional context in comments.

3 Comments

What DID it do? this definitely should make result an array rather than a string.
Kevin, dataType is a case sensitive word, so that was caught and the error was message was displayed. If I return the results using echo ($results) the result in ajax is detailed above. When I tried var obj = $.parseJJSON(result); The output was [object Object]. If I removed it, it was the same result. I was not using a double array to access the unique elements of the array. I see your point thought if nothing is returned and I will have the datatype defined. Thank you.
If you're getting [object Object], then it's working. alert(result[0].firstname)
2

In case you want to accept an answer:

Try:

alert(obj[0]['firstname']);

Comments

1

Add "results = $.parseJSON(result);" to the beginning of your success call and it should turn it into a Javascript object.

Comments

0

The PHP documentation http://php.net/manual/en/function.json-decode.php, states that when the second parameter of json_decode is true, "returned objects will be converted into associative arrays."

This should do it for you:

$results = json_decode(json_encode($results), true);
print_r($results);

2 Comments

This seems almost redundant. I don't get why you'd suggest to encode results than to decode them. Doesn't that just undo what was done? Anyhow the result was I was not treating the results returned by Ajax as a double array.
@user2672744 Did you try it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.