473,796 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3358
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='t he_page_in_ifra me.php?params=s omething';frame s['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_men u_object_id').i nnerHTML="<?=
$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.goo glegroups.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='t he_page_in_ifra me.php?params=s omething';frame s['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_men u_object_id').i nnerHTML="<?=
$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.co m 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 loadFragmentInT oElement(fragme nt_url, element_id) {
var element = document.getEle mentById(elemen t_id);
element.innerHT ML = '<p><em>Loadin g ...</em></p>';
xmlhttp.open("G ET", fragment_url);
xmlhttp.onready statechange = function() {
if (xmlhttp.readyS tate == 4 && xmlhttp.status == 200) {
element.innerHT ML = xmlhttp.respons eText;
}
}
xmlhttp.send(nu ll);
}

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.innerHTM L now.

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

<input name="criterium Submit" type="button" id="criteriumSu bmit"
value="Query" onclick="
var url='campaignif rame.php?action =searchUsers';
url+='&title='+ document.getEle mentById('title ').value;
url+='&gender=' +document.getEl ementById('gend er').value;
url+='&firstNam e='+document.ge tElementById('f irstName').valu e;
url+='&lastName ='+document.get ElementById('la stName').value;
url+='&minAge=' +document.getEl ementById('minA ge').value;
url+='&maxAge=' +document.getEl ementById('maxA ge').value;
url+='&city='+d ocument.getElem entById('city') .value;
url+='&postcode ='+document.get ElementById('po stcode').value;
url+='&info='+d ocument.getElem entById('info') .checked;
url+='&mobileNu mber='+document .getElementById ('mobileNumber' ).checked;
frames['iframe_php_res ult'].location.href= url;"/>
PHP part
=============== =============== =============== =============== =============== =====
function outputJSString( $string){

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

Javascript Part
=============== =============== =============== =============== =============== =====
<script LANGUAGE="JavaS cript">
parent.document .getElementById ('target').inne rHTML = null;
parent.document .getElementById ('target').inne rHTML='<?=
$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.co m 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
7074
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 simple/advanced html forms and how it can be used to override cookie/session
7
3418
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 are input in login screen. The password in the database is a number field. I am writing the dynamic sql statement as follows below. I believe I am going wrong in the password section of the code. I appreciate any help. Thanks. Regards.
1
17679
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 Create a Dynamic Crosstab Report PRODUCT :Microsoft Access PROD/VER:1.00 1.10 OPER/SYS:WINDOWS
7
3391
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 either AND or OR but never mixed together. We can use Northwind database for my question, it is very similar to the structure of the problem on the database I am working on. IF(SELECT OBJECT_ID('REPORT')) IS NOT NULL DROP TABLE REPORT_SELECTION
2
2945
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 sub-report will capture what grades the student has achieved in a list of different subjects and the reason I need it to be dynamic is that students take different subjects. Basically I've been trying to doctor the KB article on dynamic
9
2905
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 wants to be able to check off anywhere between 1 and all 10 fields in a form and have it return a select query with just the fields that were checked off. There are multiple users, so not all users will be checking off the same fields. Some...
10
4939
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 called, an example of what I have in mind is index.php?page=). However, I can not figure out how to do this. I will eventually want to use SEF urls, but for now I'll be content just to have the dynamic urls. If anyone can tell me how to do this, I'd...
3
18720
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, working with complex reports is tricky Assumption: Reader of this article have basic knowledge of creating data reports. Creating a Parent-Child Command and create a DataReport Suppose we have a database called company with two tables ...
5
2691
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 works fine in IE7 and beta IE8. My pages do not work however in Safari or Firefox, and please dont beat me, but i use Frontpage to write my site, and i use iframes as ive not discovered how to do this any differently! So the issue is this, i have a...
0
9680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10456
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10230
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7548
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5442
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.