473,397 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Javascript Dynamic PHP database Query

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
6 3330
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gowhera Hussain | last post by:
Use This for Learning Only .... Do Not Try To Act Smart HACKING WITH JAVASCRIPT Dr_aMado Sun, 11 Apr 2004 16:40:13 UTC This tutorial is an overview of how javascript can be used to bypass...
7
by: Jack | last post by:
Hi, I am trying to test a sql statement in Access which gives me the error as stated in the heading. The sql statement is built as a part of asp login verification, where the userid and password...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
2
by: deejayquai | last post by:
Hi I'm trying to produce a report based on a dynamic crosstab. Ultimately i'd like the report to actually become a sub report within a student end of year record of achievement. The dynamic...
9
by: Jimbo | last post by:
Hello, I have a user request to build a form in an Access database where the user can check off specific fields to pull in a query. For example, let's say I have 10 fields in a table. The user...
10
by: jflash | last post by:
Hello all, I feel dumb having to ask this question in the first place, but I just can not figure it out. I am wanting to set my site up using dynamic urls (I'm assuming that's what they're...
3
by: creative1 | last post by:
Here is how you create a complex data report that involves parent and child commands and you can update information at runtime. Its pretty straight forward to work with simple queries; however,...
5
by: loveshack | last post by:
Can anyone help me please (i am quite a novice, but having fun learning). Im not sure if this is an ASP problem, a javascript problem or a browser problem. Firstly, everything i have written...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.