1

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

4
  • 2
    Use JSON.stringify() and JSON.parse() to create and read JSON, don't try to do it by hand. Commented Jun 27, 2013 at 1:45
  • On the PHP side have you looked at json_encode() for returning JSON to the caller. Commented Jun 27, 2013 at 1:46
  • Just realized this is a duplicate: json-object-value-from-php Commented Jun 27, 2013 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. Commented Jun 27, 2013 at 20:14

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

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.
0

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

Comments

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.