By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,960 Members | 2,232 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,960 IT Pros & Developers. It's quick & easy.

Javascript Dynamic PHP database Query

P: n/a
Hi.

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?

Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
onClick="url='the_page_in_iframe.php?params=someth ing';frames['your_iframe_id'].location.href=url";
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:
parent.document.getElementById('your_main_menu_obj ect_id').innerHTML="<?=
$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:(

Jul 17 '05 #2

P: n/a
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
Javascript.
Regards,
Ramiro Varandas Jr

Jul 17 '05 #3

P: n/a
"Allan Sun" <su*****@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
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
onClick="url='the_page_in_iframe.php?params=someth ing';frames['your_iframe_i
d'].location.href=url"; 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:
parent.document.getElementById('your_main_menu_obj ect_id').innerHTML="<?=
$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:(


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 http://www.conradish.net/bobo/
along with a little demo. The class is called Pajama (PHP -> Javascript =
PJ, get it?).

Jul 17 '05 #4

P: n/a
ub****@gmail.com wrote:
Hi.

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?


XMLHttpRequest gives you the power. Google it.
Jul 17 '05 #5

P: n/a
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>';
xmlhttp.open("GET", fragment_url);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
element.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
}

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';
url+='&title='+document.getElementById('title').va lue;
url+='&gender='+document.getElementById('gender'). value;
url+='&firstName='+document.getElementById('firstN ame').value;
url+='&lastName='+document.getElementById('lastNam e').value;
url+='&minAge='+document.getElementById('minAge'). value;
url+='&maxAge='+document.getElementById('maxAge'). value;
url+='&city='+document.getElementById('city').valu e;
url+='&postcode='+document.getElementById('postcod e').value;
url+='&info='+document.getElementById('info').chec ked;
url+='&mobileNumber='+document.getElementById('mob ileNumber').checked;
frames['iframe_php_result'].location.href=url;"/>
PHP part
================================================== ==============================
function outputJSString($string){

//make it Javascript compatitable
$string=str_replace("\n","",$template->fetch());
$string=str_replace("\s","",$string);
$string=str_replace("\t","",$string);
$string=str_replace("\r","",$string);
$string=str_replace("\"","\\\"",$string);
$string=str_replace("'","\'",$string);
// and send the results
return trim($string);
}

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

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

Jul 17 '05 #6

P: n/a
ub****@gmail.com wrote:
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?


http://jibbering.com/2002/4/httprequest.html
--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.