0

I'm using a digital signage player that allows me to use HTML and javascript to display things , in my case I'm building a menu board with a list of products and prices. I've already created a php system to create entries for products and prices and those values are stored in a MySQL database. What I would like to do is have a PHP script output the mysql data in JSON format, then have a cross domain ajax request for the products and prices to display on the menu board. Unfortunately the menu board cannot support PHP so I have to improvise and get the data through JSON. I've been pulling my hair the past couple of hours trying to figure this out but I can't seem to get it. Any help would be appreciated.

EDIT 1

I have a php script and html file but can't seem to have the data show. Here are the files below

showjson.html

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$.ajax({
 url:"http://localhost/blakewilson/api.php",
 dataType: 'jsonp', // Notice! JSONP <-- P (lowercase)
 success:function(json){
     // do stuff with json (in this case an array)
     $("#userdata tbody").html("");
    $.getJSON(url,function(data){
     $.each(data.members, function(i,user){
    var tblRow =
    "<tr>"
    +"<td>"+user.postID+"</td>"
    +"<td>"+user.postProduct+"</td>"
    +"<td>"+user.postPrice+"</td>"
    +"</tr>" ;
    $(tblRow).appendTo("#userdata tbody");
},
 error:function(){
     alert("Error");
}      
});
</script>

api.php

<?php
$link = mysql_pconnect("localhost", "root", "") or die("Could not connect");
mysql_select_db("dn_name") or die("Could not select database");
$arr = array();
$rs = mysql_query("SELECT * FROM products");
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
echo $_GET['callback']."(".json_encode($arr).");";  // 09/01/12 corrected the statement
?>

EDIT 2

This code give me an alert with success so I know it works.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery PHP Json Response</title>
<style type="text/css">
</style>
</head>
<body>
<div id="msg"></div>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$.ajax({
     url:"http://localhost/blakewilson/api.php",
     dataType: 'jsonp', 
     success:function(json){
        alert("Success");
    },
     error:function(){
         alert("Error");
    }      
});
</script>
</body>
</html>

I'm trying to get a few values like postID, postProduct, and postPrice from JSON, but I can't seem to figure it out. I'm very new to jQuery/AJAX etc.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery PHP Json Response</title>
<style type="text/css">
</style>
</head>
<body>
<div id="msg"></div>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$.ajax({
     url:"http://localhost/blakewilson/api.php",
     dataType: 'jsonp', 
     success:function(json){
        // loop through the productg here
        $.each(json.members,function(i,dat){
        $("#msg").append(
        '<div class="members">'+
        '<h1>'+dat.postID+'</h1>'+
        '<p>Firstname : <em>'+dat.postProduct+'</em>'+
        '<p>SurName : <em>'+dat.postPrice+'</em></p>'+
        '<hr>'+
        '</div>'
        );
        });
    },
     error:function(){
         alert("Error");
    }      
});
</script>
</body>
</html>

EDIT 3

What I'm basically trying to do is take this project: http://tournasdimitrios1.wordpress.com/2011/11/04/how-to-generate-json-with-php-from-mysql-and-parse-it-with-jquery/ and make it work cross domain. That is all I need.

EDIT 4

This is the output of the api.php file. It's throwing an error "Notice: Undefined Index"

Notice: Undefined index: callback in E:\xampp\htdocs\blakewilson\api.php on line 13
([{"postID":"8","postProduct":"Synthetic Oil Change","postDollar":"29","postCents":"95","postDate":"2014-08-12 12:11:00"},{"postID":"9","postProduct":"Tire Rotation","postDollar":"16","postCents":"95","postDate":"2014-08-12 12:11:10"},{"postID":"10","postProduct":"Rotate and Balance","postDollar":"39","postCents":"95","postDate":"2014-08-12 12:11:21"},{"postID":"11","postProduct":"4-Wheel Alignment","postDollar":"79","postCents":"95","postDate":"2014-08-12 12:11:35"},{"postID":"12","postProduct":"Cooling System Service","postDollar":"129","postCents":"95","postDate":"2014-08-12 12:11:51"},{"postID":"13","postProduct":"Transmission Flush","postDollar":"189","postCents":"95","postDate":"2014-08-12 12:12:07"},{"postID":"14","postProduct":"AC Performance Service","postDollar":"69","postCents":"95","postDate":"2014-08-12 12:12:19"}]);
Blake
  • 561
  • 5
  • 14
  • Javascript can't do cross domain ajax requests. The target url has to be the same domain (maybe someone knows if this is also true for requests to subdomain). – Charlotte Dunois Aug 13 '14 at 18:26
  • I didn't mean it like that, my player can render ajax and jquery so that's no issue. It just needs cross domain because the html page that will display the prices and products will be on the player @charlotte-dunois – Blake Aug 13 '14 at 18:46
  • you do know that `$.getJSON` do is an AJAX call so you are technicaly doing an AJAX call within an AJAX success callback? – Sebastien Aug 13 '14 at 19:36
  • Yes i realized that, I accidentally posted that in. @Sebastien – Blake Aug 13 '14 at 19:37
  • have you tried to `console.log(json);` to see if the success callback it triggered somewhere along the execution and to see if you even receive data in your `json` parameter. – Sebastien Aug 13 '14 at 19:41
  • And your javascript/jquery snippet you post is horrible it misses most of the closing `}` could you check it and clean it up so it would be easier to help and spot the problem? – Sebastien Aug 13 '14 at 19:43
  • @Sebastien Yeah what I did was put an alert on success and the alert came through with the proper text, so I know it works. I'm just having a hard time displaying the data. – Blake Aug 13 '14 at 19:44
  • Could you post the above mention code as well as the result you got from your alert? – Sebastien Aug 13 '14 at 19:45
  • Just updated @Sebastien, let me know if you need anything else, thanks for your help. – Blake Aug 13 '14 at 19:54
  • @BlakeWilson Could you replace `success:function(json){ alert("Success");}` with `success:function(json){console.log(json);}` once the code is change on your browser hit F12 to see your browser's console and tell me if `json` is empty. – Sebastien Aug 13 '14 at 20:01
  • this is what it spits out @Sebastien – Blake Aug 13 '14 at 20:06
  • '[Object, Object, Object, Object, Object, Object, Object] 0: Object 1: Object 2: Object 3: Object 4: Object 5: Object 6: Object length: 7 __proto__: Array[0]' @Sebastien – Blake Aug 13 '14 at 20:06
  • @BlakeWilson in response to your EDIT#3 your call does seems to work just fine 'crossdomain' (since you receive data in your success callback). I think its just the javascript parsing on success that dont work. – Sebastien Aug 13 '14 at 20:15
  • I agree with you @Sebastien do you know how I would go about parsing the json values into the success callback? – Blake Aug 13 '14 at 20:17
  • Well that depends what json you sent server side. what is the result of your `echo` in `api.php`? – Sebastien Aug 13 '14 at 20:22
  • @Sebastien check out edit 4, that is the output on api.php it creates the json but also throws an error – Blake Aug 13 '14 at 20:27
  • I would apply line 4 of @ChrisN answer. the part where you `echo json_encode($arr);` only. – Sebastien Aug 13 '14 at 20:31
  • @Sebastien once I do that the showjson.html page throws error instead of success – Blake Aug 13 '14 at 20:34
  • what error do you get? – Sebastien Aug 14 '14 at 12:04

2 Answers2

1

the Below link works well for me, you can try

http://jquery-howto.blogspot.in/2013/09/jquery-cross-domain-ajax-request.html

0

Not sure whats in the

echo $_GET['callback']."(".json_encode($arr).");";

But you might try removing it all down to

echo json_encode($arr);

If you need to send 2 pieces of data back, just build that into another array. Such as:

echo json_encode(array('message' => $_GET['callback'], 'data' => $arr));
Chris N
  • 24
  • 3
  • Thanks @chris-n. The reason I have the callback in there is that im basically just going off this post. http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain . This works great for me, I just need to know how to enter those values in a table – Blake Aug 13 '14 at 19:40