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

Trying to pass multidimensional php array to Javascript using JSON. The code refuses to enter the success state of JSON, why is this?

This is javascript file:

array = [];

function callback(arr)
{
 console.log(array);
 //simpleText.setText = array[0]
};
$(document).ready(function() {
 $.getJSON('database.php', function(phpdata){
    console.log("po");
    console.log(phpdata);
    callback(phpdata);
  });

});

And php file:

header("Content-type: application/json");
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("atlas") or die(mysql_error()); 

$data = mysql_query("SELECT * FROM questions") 
or die(mysql_error()); 

$i = 0;
$result_array = array();
 while ($Row = mysql_fetch_array($data)) 
{ 
$user[] = array( 
'id'=>$Row['id'], 
'q'=>$Row['q'],
'a'=>$Row['a'],
'coordx'=>$Row['coordx'],
'coordy'=>$Row['coordy'],
    ); 
} 


var_dump($user);

$json = json_encode($user[0]);


echo $json;
share|improve this question
2  
Share your JSON with us –  lancemanfv yesterday
 
You're not passing a multidimensional array. You're just passing $user[0], which is one row, which is a one-dimensional array. –  Barmar 23 hours ago
1  
You need to take var_dump() out of the script. It will be read by $.getJSON, but it's not valid JSON. –  Barmar 23 hours ago

3 Answers

Remove the

var_dump($user);

line. It's making the result invalid JSON.

share|improve this answer

getJSON jQuery function does not return errors. Use ajax function below and check your error message and returned JSON (inside jqXHR object):

$.ajax({
    url: 'database.php',
    dataType: 'json',
    success: function(data, textStatus, jqXHR) {
        // success
    },
    error: function(jqXHR, textStatus, errorThrown) {
        // error
    }
});
share|improve this answer
 
It seems like i'm getting a SyntaxError{} from the errorThrown, any idea why? –  Bobbie 10 hours ago

I did this:

$.ajax({
    url: 'database.php',
    dataType: 'json',
    success: function(data, textStatus, jqXHR) {
        //console.log(textStatus);
        //console.log(jqXHR);
    },
    error: function(jqXHR, textStatus, errorThrown) {
        console.log(jqXHR);
    }
});

And i get a huge error which is kinda hard to understand what its saying.

abort: function (e){var t=e||w;return u&&u.abort(t),k(0,t),this}
always: function (){return i.done(arguments).fail(arguments),this}
complete: function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}
done: function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}
error: function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}
fail: function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}
getAllResponseHeaders: function (){return 2===b?a:null}
getResponseHeader: function (e){var t;if(2===b){if(!c){c={};while(t=Tn.exec(a))c[t[1].toLowerCase()]=t[2]}t=c[e.toLowerCase()]}return null==t?null:t}
overrideMimeType: function (e){return b||(p.mimeType=e),this}
pipe: function (){var e=arguments;return x.Deferred(function(n){x.each(t,function(t,o){var a=o[0],s=x.isFunction(e[t])&&e[t];i[o[1]](function(){var e=s&&s.apply(this,arguments);e&&x.isFunction(e.promise)?e.promise().done(n.resolve).fail(n.reject).progress(n.notify):n[a+"With"](this===r?n.promise():this,s?[e]:arguments)})}),e=null}).promise()}
progress: function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}
promise: function (e){return null!=e?x.extend(e,r):r}
readyState: 4
responseText: "array(2) {↵  [0]=>↵  array(5) {↵    ["id"]=>↵    string(1) "0"↵    ["q"]=>↵    string(7) "Moskva?"↵    ["a"]=>↵    string(12) "Vid punkten!"↵    ["coordx"]=>↵    string(2) "25"↵    ["coordy"]=>↵    string(3) "255"↵  }↵  [1]=>↵  array(5) {↵    ["id"]=>↵    string(1) "1"↵    ["q"]=>↵    string(15) "Var ligger Rom?"↵    ["a"]=>↵    string(4) "D�r!"↵    ["coordx"]=>↵    string(3) "255"↵    ["coordy"]=>↵    string(3) "255"↵  }↵}↵"
setRequestHeader: function (e,t){var n=e.toLowerCase();return b||(e=v[n]=v[n]||e,y[e]=t),this}
state: function (){return n}
status: 200
statusCode: function (e){var t;if(e)if(2>b)for(t in e)m[t]=[m[t],e[t]];else C.always(e[C.status]);return this}
statusText: "OK"
success: function (){if(l){var t=l.length;(function i(t){x.each(t,function(t,n){var r=x.type(n);"function"===r?e.unique&&p.has(n)||l.push(n):n&&n.length&&"string"!==r&&i(n)})})(arguments),n?o=l.length:r&&(s=t,c(r))}return this}
then: function (){var e=arguments;return x.Deferred(function(n){x.each(t,function(t,o){var a=o[0],s=x.isFunction(e[t])&&e[t];i[o[1]](function(){var e=s&&s.apply(this,arguments);e&&x.isFunction(e.promise)?e.promise().done(n.resolve).fail(n.reject).progress(n.notify):n[a+"With"](this===r?n.promise():this,s?[e]:arguments)})}),e=null}).promise()}

In the phpfile: I did remove var_dump($user) I did change JSON to send the entire array

My var_dump looks like this:

array(2) {
  [0]=>
  array(5) {
    ["id"]=>
    string(1) "0"
    ["q"]=>
    string(7) "Moskva?"
    ["a"]=>
    string(12) "Vid punkten!"
    ["coordx"]=>
    string(2) "25"
    ["coordy"]=>
    string(3) "255"
  }
  [1]=>
  array(5) {
    ["id"]=>
    string(1) "1"
    ["q"]=>
    string(15) "Var ligger Rom?"
    ["a"]=>
    string(4) "Där!"
    ["coordx"]=>
    string(3) "255"
    ["coordy"]=>
    string(3) "255"
  }
}

Any ideas?

share|improve this answer

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.