views:

116

answers:

5

I am a little bit new to the PHP/MYSQL arena, and had an idea to be able to interact with my Database by using a hidden Iframe to run PHP pages in the background(iframe) on events without having to leave the current page?

Good? Bad? Common Practice? Opinions?

A: 

You may want to consider XMLHttpRequest (AJAX) instead.

This is one article from 2006 that I found that discusses the advantages of using a hidden iframe instead of XMLHttpRequest (also featured on the Ajaxian). I am of the opinion that those arguments are not applicable or relevant today (and probably weren't in 2006 as well).

Daniel Vassallo
A: 

This is commonly accomplished using AJAX. The jQuery javascript library makes this easy

I don't think using iframes is a good way to accomplish this. You would still need javascript enabled to change the location of the iframe, and if javascript is available, why not just use AJAX?

If you use the iframe, you wouldn't be able to receive a response from the server in any meaningful way without doing a lot of workarounds. For example -- using jQuery, you could submit some information to the server with a single function call, and then when that request completes, a callback function can be invoked with response information from the server:

$.post("ajax.php", { var1: "data", var2: "moredata" },
   function(data){
     alert("Server says: " + data);
   });

In this example, when the request completes, an alert box appears with the output of ajax.php.

With an iframe, you might do something like change the location of the iframe to server.com/iframe.php?var=data&var2=moredata&var3=moredata, then wait for a bit, and grab the contents of the iframe (if this is even possible) and do something with that.

Not to mention, when you run into problems doing this, you'll probably ask for advice on SO. and each time, people will probably say "drop that and use jQuery!" :) may as well skip all the pain and suffering and do it the Right Way to begin with

Carson Myers
A: 

I'd guess something is supposed to happen when your database does something, right? I.e. your page should give some sort of feedback, maybe update a number or some text.

So you're going to use Javascript anyway. In that case, skip the iframe and just send off AJAX requests.

deceze
+2  A: 

This is most of the time bad, but sometimes inevitable.

The common practice to do it is to use AJAX, it's so common that even W3School has an article about it.

The advantages of using AJAX over IFrame is that AJAX can be multi-threaded. You can send several requests in a row, which is more troublesome to implement with IFrames. Moreover, AJAX supports status code so you can detect errors, where with IFrames you'd have to rely on scraping the page's HTML and hope you've determined the correct status by looking at the error page's HTML code.

AJAX is more JavaScript idiomatic and event driven, which means your callback will get notified automatically when there is a response. With IFrame you'd have to setTimeout() and keep polling the IFrame for a response, which may break easily.

IFrame is sometimes inevitable in cases like where you want to upload a file without leaving the current page. But that's probably not your scope since you mentioned only database interactions.

Learn to use XMLHttpRequest, which is the foundation of AJAX. After you've become familiar with that, try making it fun by using a JavaScript framework such as jQuery, Dojo, etc.

kizzx2
There's no need to keep polling the iframe with `setTimeout()`. You could simply send JavaScript code in the iframe, which can execute a callback when it loads.
Daniel Vassallo
A: 

The hidden iframe method was used before the adoption of XMLHttpRequest api (Maybe you have heard of it as Ajax).

Years ago I was using a former implementation using rslite but nowadays this technique has, to me, just an historical value.

You can get directions on using Ajax techniques in plain javascript at http://www.xul.fr/en-xml-ajax.html or, better, you can choose to use a common library, jquery or mootools among others, to deal with the different implementations in different browser.

Eineki