JavaScript and MySQL

J

Jens Körte

Hi!

I've got a typical newbie-problem, I guess.
I got some selectboxes on my site. these boxes get their data from a
databse. everything works fine, but when a user selects a value from
one of the boxes i want to change the values from the other boxes
dynamically. The values are derived from the database. Initially the
boxes get for example those inputs:
Box1 --> SELECT * FROM VEHICLE
Box2 --> SELECT * FROM CAR

If a user selects "car" from Box1, Box2 must change his content so
that only cars are shown, i.e. no trucks, no bicylces, etc...
Something like that:
SELECT c.name FROM CAR c WHERE VEHICLE = 'car'

With the onChange-event I can handle the values. No problem with that.
But how do I get the data from the database into the
javascript-statement. How do I have to handle this. I have no idea.
Please help...


Jens

P.S. I only want to use PHP, HTML and Javascript.
 
J

Joakim Braun

Jens Körte said:
Hi!

I've got a typical newbie-problem, I guess.
I got some selectboxes on my site. these boxes get their data from a
databse. everything works fine, but when a user selects a value from
one of the boxes i want to change the values from the other boxes
dynamically. The values are derived from the database. Initially the
boxes get for example those inputs:
Box1 --> SELECT * FROM VEHICLE
Box2 --> SELECT * FROM CAR

If a user selects "car" from Box1, Box2 must change his content so
that only cars are shown, i.e. no trucks, no bicylces, etc...
Something like that:
SELECT c.name FROM CAR c WHERE VEHICLE = 'car'

With the onChange-event I can handle the values. No problem with that.
But how do I get the data from the database into the
javascript-statement. How do I have to handle this. I have no idea.
Please help...


Jens

Unless you want to do a reload whenever Box1 changes (and write the
<options> directly server-side), you can write a dynamic Javascript with,
for instance, PHP. Something like this (PHP pseudo-code, but you get the
idea):

function writeDynamicJavascript(){

$result = 'function box1changed(inNewValue){' . "\n";
$box1Values = /* get possible values from MySQL as PHP array */;

$result .= 'var box2Values = new Array();' . "\n";
$result .= 'var ctr = 0;' . "\n";
$result .= 'switch(inNewValue){' . "\n"

foreach($box1Values as $box1Value){
$result .= 'case "' . $box1Value . '":' . "\n";
/* fill the JS theResult variable of the dynamic JS function with
values box2 can take */
/* if box 1:s value is $box1Value - a loop something like:
$result .= 'box2Values[ctr++] = "' . /* some value */ . '";' . "\n";
$result .= 'break;' . "\n";

}

$result .= '}' . "\n"; // close JS switch

/* Add JS code here to reload box2 from the array elements now in
box2Values */

$result .= '}' . "\n"; // close JS function
return $result;
}

You echo the result of writeDynamicJavascript() into the <head> of the
document. Then you add onchange="box1changed(this.value);" (the name of the
JS function that writeDynamicJavascript() generates) to box 1.

Note that if either of the <select> boxes can take a lot of values, this may
result in an arbitrarily large Javascript in the <head>... But for a limited
number of choices, it works well.

Joakim Braun
 
J

Joakim Braun

Joakim Braun said:
Unless you want to do a reload whenever Box1 changes

<snip>

What I meant was: Unless you want to post the form and regenerate the
document whenever Box1 changes...

Joakim Braun
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top