Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm making an aplication with phonegap and I'm stuck trying to send JSON data from the PHP on the server to JavaScript on the device. I want to do something like:

var JSON = '{ "Table" : ' + "http://www.hel.net/LoadDB.php=?table=exhibitions" +  '}';

the php works fine and returns somethig like:

"[{"id":"1","name":"Art for everyone","image":null,"name2":"June 29, 2013: 11:00am.","description":"With reservation\r\nFree entrance","description2":null}]"

I want that result in a javascript variable to work later with:

var obj = eval ("(" + JSON + ")");
document.getElementById("rName").innerHTML=obj.Table[1].name;
document.getElementById("lname").innerHTML=obj.Table[1].name2; 

What I want to do is something like:

var JSON = '{ "Table" : ' + "http://www.hel.net/LoadDB.php=?table=exhibitions" +  '}';
var obj = eval ("(" + JSON + ")");
document.getElementById("rName").innerHTML=obj.Table[1].name;
document.getElementById("lname").innerHTML=obj.Table[1].name2; 

How can I make the first line work? is it possible to make the first line work? PS. I do not have much experience with JSON arrays.



Ok I tried ajax and works, I used:

console.log("before"); 

var jqxhr = $.ajax( "http://www.hel.com/LoadDB.php?table=exhibitions" )
            .done(function(data) { console.log(data); })
            .fail(function() { console.log("error"); })
            .always(function() { console.log("complete"); });

console.log("after");

more info in:

api.jquery.com

share|improve this question
1  
Use JSON.stringify() and JSON.parse() to create and read JSON, don't try to do it by hand. –  Barmar Jun 27 '13 at 1:45
    
On the PHP side have you looked at json_encode() for returning JSON to the caller. –  asafreedman Jun 27 '13 at 1:46
    
Just realized this is a duplicate: json-object-value-from-php –  TecBrat Jun 27 '13 at 18:16
    
Looks like my question. My problem is not evaluating the JSON is getting the response from the PHP, later I will evaluate it. I already use .get and .getJSON, didn't work. Something is missing. –  Cesapr Jun 27 '13 at 20:14

3 Answers 3

I think all you need is var obj = <?php echo $myjsonencodedvar; ?>

or

var obj = <?php echo json_encode($myarray_or_object); ?>

Since I said "I think..." I decided to test it out. I found the following dump() function here on SO.

$arr=array(1,'biscuit'=>'gravy',3,4,5);
$json=json_encode($arr);
?>
<script>
  j=<?php echo $json; ?>;
 document.write(dump(j));

 function dump(obj) {
    var out = '';
    for (var i in obj) {
        out += i + ": " + obj[i] + "\n";
    }

    return out;
}
</script>

output:

0: 1 biscuit: gravy 1: 3 2: 4 3: 5
share|improve this answer

using JSONP (no callbacks), and on the client side use $.getJSON() it will parse it from json string to js object.

share|improve this answer
    
He's not using jQuery. –  Barmar Jun 27 '13 at 2:07
    
oops .. thanks for notifying –  CME64 Jun 27 '13 at 2:08

Try this:

PHP: (json.php)

<?php
    header("Content-Type: text/json");

    //the data format your question mentioned
    $data = array("Table"=>array(array("id"=>"1","name"=>"Art for everyone","image"=>null,"name2"=>"June 29, 2013","description"=>"With reservation\r\nFree entrance","description2"=>null)));

    echo json_encode($data);
?>

Front-end:

<!DOCTYPE html>
<html>
<head>
 <title></title>
</head>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
    $.get("json.php",function(json){
        alert(json.Table[0].name);
    });
</script>
</body>
</html> 

Hope this is helpful for you.

share|improve this answer
    
the echo now works great, thanks @Chickenrice. –  Cesapr Jun 27 '13 at 15:08
    
In the dbSelect.js I’m coding: console.log("before"); $.get("http://www.hel.net/LoadDB.php=?table=exhibitions", function(json){ console.log(json.table[0].name); console.log("in code"); } );<br/> console.log("after"); In the log I receive the “before” and the “after” but never the “in code”. I think the problem is something else. –  Cesapr Jun 27 '13 at 15:40

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.