...

View Full Version : Dynamic load selectboxes help



MarioPro
07-27-2002, 07:09 PM
Hi,

I'm having a problem o coding a feature that would enable to dynamically load selectboxes using PHP and Javascript.

My problem is on Javascript and not on PHP.

Thanks in advance for any help.

MarioPro

Here's what I would like to do:

<!--### The Data ###-->
Populate selectboxes dynamically for the following information, which is directly related in my database with ID's.
HTML
1st. Selectbox: County
2nd. Selectbox: City
3rd. Selectbox: Location

PHP/MySQL
$county_ID (Table counties)
$county (Table counties)
$city_ID (Table cities)
$city (Table cities)
$location_ID (Table locations)
$location (Table locations)

<!--### The Objective Explained ###-->
Once the user selects the County, the City and Location selectboxes would be populated with the data corresponding to the County selected, without reloading the page.

<!--### The Main SelectBox Schema ###-->

Select your county:
<SELECT NAME="cnt[county]">
<OPTION VALUE="Any">Any</OPTION>
<?
// Selects the county and populates it even
// if the table is updated
$query = "SELECT * FROM counties";
$result = mysql_query($query);
while($rowCnt = mysql_fetch_array($result)){
echo '<OPTION VALUE="'.$rowCnt['ID'].'" ';
// Set the selected county from the earlier search if any
if($rowCnt['ID'] == $cnt['county']){
echo ' SELECTED ';
}
echo '>'.$rowCnt['county'].'</OPTION>'."\n";
}
?>
</SELECT>

Select your City:
<SELECT NAME="c[city]">
<OPTION VALUE="Any">Any</OPTION>
<?
// Selects the city and populates it even
// if the table is updated
$query = "SELECT * FROM cities WHERE county_ID='".$f['county']."'";
$result = mysql_query($query);
while($rowC = mysql_fetch_array($result)){
echo '<OPTION VALUE="'.$rowC['ID'].'" ';
// Set the selected city from the earlier search if any
if($rowC['ID'] == $c['city']){
echo ' SELECTED ';
}
echo '>'.$rowC['city'].'</OPTION>'."\n";
}
?>
</SELECT>

Select your City:
<SELECT NAME="l[location]">
<OPTION VALUE="Any">Any</OPTION>
<?
// Selects the location and populates it even
// if the table is updated
$query = "SELECT * FROM locations WHERE city_ID='".$f['city']."'";
$result = mysql_query($query);
while($rowL = mysql_fetch_array($result)){
echo '<OPTION VALUE="'.$rowL['ID'].'" ';
// Set the selected location from the earlier search if any
if($rowL['ID'] == $l['location']){
echo ' SELECTED ';
}
echo '>'.$rowL['location'].'</OPTION>'."\n";
}
?>
</SELECT>

bpaulsen2000
07-27-2002, 09:39 PM
Hi,

http://javascriptkit.com/cutpastejava.shtml

look at double combo scripts

MarioPro
07-27-2002, 09:44 PM
Hi,

Thanks for the tip. This might be useful for small arrays. Now imagine that one of mine - $cities, I have 308 names, joining with about 23 counties and 11 locations, this would become and enormous load on the page, eve if called as a js include.

My objective was to populate the selectboxes with only the correspondent data, which would result on smaller arrays for the combos.

Any way thanks for the valuable help.

MarioPro

MarioPro
07-28-2002, 01:41 PM
Hi,

Has anyone seen this type of combo or selectboxes functioning with Javascript + PHP?

Thanks in advance for any help.

MarioPro

RadarBob
07-28-2002, 05:30 PM
Thanks for the tip. This might be useful for small arrays. Now imagine that one of mine - $cities, I have 308 names, joining with about 23 counties and 11 locations, this would become and enormous load on the page, eve if called as a js include.


Not as much as you might think. The overall "work" the page has to do might be significant- this one piece won't be overly slow I strongly suspect.


BUT if it worries you, I suggest you do it from another window - that's what I did. I have a dynamic list, and to add/delete from it I have a button that opens another window and the list is passed to that window and edited from there. Click on "finish" and it's passed back to the parent window.

This technique eliminates the overhead you're worried about from the parent page and it's only invoked when the user asks for it (clicks on the button).

MarioPro
07-29-2002, 01:10 AM
Hi,

Once again, thanks for your reply.

Your suggestion of oppening another window seems to be very handful, and user friendly as well.

Do you have any link where I may see some code of that? I'm not as good (in fact a newbbie) on Javascript as in PHP/MySQL.

Thanks!
:thumbsup:

MarioPro



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum