Javascript Dynamic PHP database Query

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm wondering if you can dynamically query a database with javascript
and php.  For example, can I select something on the select menu and
dynamically query the database and populate another select box without
reloading the page?

Re: Javascript Dynamic PHP database Query

This is how I would do it:

1. Insert a iframe with height=0 and border=0 in your php file.
2. In your  menu, set the object to be clicked to have the
3. In your iframe php file, query the database and get the result, add
them with escape charactor "\" for every thing Javascript needed, clean
the \n .
4. In your iframe php file, write the js as:
$what_you_just_got ?>";
5. Then you'll see it!

I can give you more codes tommorrow when I get back to my office, if
you feel they are useful.

I would very much like to see how they other people are dealling with
it, coz I even thought it's a bit complex:(

Re: Javascript Dynamic PHP database Query

I would like to view more codes of this if you have, because I tried
this so much and never could make it. I'm not so good with Iframes and
  Ramiro Varandas Jr

Re: Javascript Dynamic PHP database Query

Quoted text here. Click to load it
Quoted text here. Click to load it

A while back when I was playing around with Flash and AMFPHP, I wrote a
little class that let you retrieve data from a remote server in a manner
similiar to Flash Remoting in a HTML page. Then I promptly forgot about it.

The method used is the same as the one you described, except the iframe is
created dynamically, a post done instead, and the iframe is destroyed when
afterward. This last step turned out to be critical for otherwise the link
to the iframe page will linger in the history, messing up the back/forward
button navigation.

The class let you call a server method with any number of parameters, all of
which can be complex objects. The method can also return a complex object.
The class traps PHP errors, sending messages back to Javascript so you can
throw them up on an alert box. Pretty neat stuff.

I've just put it up on my personal page at /
along with a little demo. The class is called Pajama (PHP -> Javascript =
PJ, get it?).

Re: Javascript Dynamic PHP database Query wrote:
Quoted text here. Click to load it

<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com    Blog: /

Re: Javascript Dynamic PHP database Query wrote:
Quoted text here. Click to load it

XMLHttpRequest gives you the power. Google it.

Re: Javascript Dynamic PHP database Query

Yes XMLHttpRequest is really a good idea! But sometimes I just found it
not that good for debugging:P
For those who would still go XMLhttpRequest, here is a piece of very
nice and clean code

function loadFragmentInToElement(fragment_url, element_id) {
var element = document.getElementById(element_id);
element.innerHTML = '<p><em>Loading ...</em></p>';"GET", fragment_url);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
element.innerHTML = xmlhttp.responseText;

For what I've done, it looks a bit more comlex than XMLHttpRequest,
maybe I should say it's really worth than XMLHttpRequest, but I'll
still put it here. Because most of the browsers support Iframe and
object.innerHTML now.

HTML part:
<iframe src="about:blank" height="0" width="100%" frameborder="1"
id="iframe_php" name="iframe_php">Sorry your browser doesn't support
IFRAME, this programme may not work properly.</iframe>

<input name="criteriumSubmit" type="button" id="criteriumSubmit"
value="Query" onclick="
var url='campaigniframe.php?action=searchUsers';
PHP part
function outputJSString($string){

//make it Javascript compatitable
// and send the results
return trim($string);

Javascript Part
<script LANGUAGE="JavaScript">
parent.document.getElementById('target').innerHTML = null;
$view->outputJSString() ?>';

Quite a lot ham? Franckly I would go for XMLHttpRequest if I don't need
to consider the browser campabilities for now:)

Site Timeline