0

Completely new to using AJAX and JSON with PHP and MYSQL. The code I have works with localhost, but returns nothing from the PHP files on 000webhost. Just wondering if there is any essential bits of code i'm missing out on here for an external server, or whether i have to add in extra code because of 000webhost itself (Although I don't imagine this being the case). Here is the code for one of my PHP files:

<?php

include 'dbconfig.php';

$con = mysql_connect($dbhost,$dbuser,$dbpass);
$dbs = mysql_select_db($dbname, $con);

$result = mysql_query("SELECT * FROM `desserts`");

$data = array();
while ( $row = mysql_fetch_row($result) )
{
  $data[] = $row;
}
echo json_encode( $data );
?>

(This is just a test file, so i'm not worried about sql injections or anything of the sort)

And here is the JavaScript code to retrieve the data:

$.ajax({                                      
                          url: 'http://appname.net/PHP/getDesserts.php',
                          data: "data",            

                          dataType: 'json',             
                          success: function(rows) 
                          {
                              for(var i in rows)
                              {
                                    var row = rows[i];

                                    var startname = row[1];
                                    var startprice = row[2];
                                    var startpagelink = row[4];

                                    $('#main_content').append('<b><a href="'+startpagelink+'">'+'<img id="sammich" src="sammich.jpg">'+'</br>'+startname+'</br><font color="400000">£'+startprice)
                                    .append("</font><hr /></a></b>"); 
                            }
                          } 
                        });

As i said this code works with localhost, but when I moved the php files to the server and changed the url to suit, is when i began to get this problem. Any help would be greatly appreciated.

1 Answers1

0

Use JSONP, which is a method to request data from a server in a different domain.

Update your backend script like this. (You should probably also add something to verify that $_GET['callback'] is a valid javascript function name).

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

And the client script like this.

$.ajax({                                      
   url: 'http://appname.net/PHP/getDesserts.php',
   dataType: 'jsonp',
Zoredache
  • 37,543
  • 7
  • 45
  • 61
  • Thanks for the reply, just gave this a shot but there is still nothing getting sent back from the database. – user2226200 Apr 02 '13 at 19:52
  • Are you sure the request is being made properly? Check your web server access logs. Fire up a wireshark/tcpdump session. What happens if you open up a web browser and go to `http://appname.net/PHP/getDesserts.php?callback=foo`? – Zoredache Apr 02 '13 at 20:17
  • Because it's a free hosting service, i don't think i have access to the server logs. I'll need to figure out how to use wireshark/tcpdump as well. – user2226200 Apr 02 '13 at 20:29
  • Tried the ?callback there, this is what i got in return: foo([["1","Cheese Ploughmans Sandwich","3.45","Yes","lentil.html","0"],["2","Pan-fried Garlic Mushrooms","4.00","No","mushrooms.html","0"],["3","Nachos","4.50","No","nachos.html","0"]]); – user2226200 Apr 02 '13 at 20:32
  • That is a good sign. It looks like you are returning what you should be, I am not sure why your ajax is failing. Are you sure there are no errors showing up in javascript console? – Zoredache Apr 02 '13 at 20:39
  • There are no errors showing up on the page, or in my code editor. Could it have something to do with the url? Like, maybe using an IP address instead (Sorry, still getting used to this). – user2226200 Apr 02 '13 at 21:15
  • Debugged the page using firebug there, it's giving an error on the getDesserts.php saying ` ` The – user2226200 Apr 02 '13 at 23:41
  • Ah, that seems to indicate that your free hosting provider is injecting crap into your script output. If that is the case you are kind of out of luck. – Zoredache Apr 02 '13 at 23:48
  • Ok, looks like this particular bit of script is the source of a few problems online and is most likely interfering with the code i'm running. Looks like the webhost is actually causing the problem [as seen here](http://stackoverflow.com/questions/2268868/webhoster-inserts-a-javascript-which-brokes-my-code-how-to-remove-it) – user2226200 Apr 02 '13 at 23:55