views:

344

answers:

1

I have been making extensive use of XMLHttpRequests and JSON to fetch from a MySQL database and return records as arrays. It works perfectly.

Additionally, I have three cases in which I have the server (via PHP) formatting the data as a web page and creating bar charts (as opposed to sending arrays back to JavaScript for processing). Currently, I call the PHP file via a hidden iframe and then insert the HTML into a DIV (since I don't like certain aspects of iframes and prefer to not have them accessible).

Is using an iframe the best method or can I accomplish the same thing via a XMLHttpRequest and JSON? I'm asking because of the length of the data being returned. At present, it translates to a full page of data, but I want it to be extensible (e.g., perhaps a couple of pages of data in the future).

Thank you for any insights.

+2  A: 

If you want to get formatted html from other page and insert it in the current page dom the easiest way according to me is using the jQuery load methods. It make ajax request to a given url, gets the html and appends it to specified page element.

Generally the two methods - passing JSON data and passing Formatted html are good for different things. I personally prefer to pass JSON data and format it with client script and html. In this way it is more reusable.

Branislav Abadjimarinov
correct, jquery can do the same, but the concern is, length of data cannot be reduced. data can only be reduced if PHP response part of the content, that is, need some code change in PHP anyway.
joetsuihk
You can always Gzip and\or cache the data if it is fairly static so length will not be that much of a problem I think.
Branislav Abadjimarinov
Thank you Branislav (and also Joel for the size reminder). I'll give JQuery a try. Your comment, about preferring to pass JSON data, for client formatting, is consistent with something I have been wondering (avoiding): rewriting my PHP function in JavaScript. The beginning code started as JavaScript and then I switched to PHP before making it more elaborate (complicated). I switched because the client device is likely a smart-phone and I didn't want to depend on its processing power. But, using a single method (JSON), throughout my code, would be nice.
Alan Neal