/    Sign up×
Community /Pin to ProfileBookmark

how to build drop down menu that has values that change depending on another menu

Hi,

I would like to know how to build two drop down menus so that the value of the second drop down menu changes depending on what is selected in the first.

The values in both drop down menus, however, need to come from a database because they need to be updated. Does this require some lind of javascript array? I am not sure how to go about this.

can anyone explain how to do this or have any example code,

any help will be much appreciated,

thanks a lot

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@scragarMar 30.2005 — [code=php]<script type="text/javascript">
var itemSplit = ",";
var groupSplit = ";";
var strFdrop = "1-1,1-2,1-3;2-1,2-2,2-3;3-1,3-2,3-3";
var i;
var strFdrop = strFdrop.split(groupSplit);
for(i = 0; i < strFdrop.length; i++){
strFdrop[i] = strFdrop[i].split(itemSplit);
};

function UpdateDropdown(){
for(i = 0; i < strFdrop[dropDown1.selectedIndex].length; i++){
dropDown2.options[i].text = strFdrop[dropDown1.selectedIndex][i];
dropDown2.options[i].value = strFdrop[dropDown1.selectedIndex][i];
};
};
</script>
<select name="dropDown1" onchange="UpdateDropdown()"><option value="x">1</option>
<option value="n">2</option>
<option value="q">3</option></select>
<select name="dropDown2"><option></option><option></option><option></option></select> [/code]
Copy linkTweet thisAlerts:
@damon2003authorApr 02.2005 — thanks for the reply,

I tried this but it doesnt appear to work yet, have you tested this code,

also how do you get the values from the database into the javascript array,

thanks a lot for any replies
Copy linkTweet thisAlerts:
@damon2003authorApr 05.2005 — thanks,

but that thread does'nt explain how to get the value from the database into a javascript array, I am not sure how to do this,

any ideas there?

thanks a lot for any replies
Copy linkTweet thisAlerts:
@phpnoviceApr 05.2005 — Client-side JavaScript can't do anything with databases -- at least, not directly. You'll have to decide on a server-side language for that part and post that question into the appropriate forum.
Copy linkTweet thisAlerts:
@WebskaterApr 05.2005 — What server side language are you using? I have asp examples.
Copy linkTweet thisAlerts:
@damon2003authorApr 05.2005 — hi,

I am actually using php,

but any examples will be helpful if you have any,

thanks
Copy linkTweet thisAlerts:
@scragarApr 05.2005 — [code=php]
<script type="text/javascript">
<!--
<?
/* asuming the table:
+-----tblMenu------+
| LinkText |
| LinkTo |
| group |
+------------------+*/
echo 'var strFdrop = "';
$sql = "SELECT * FROM tblMenu ORDER BY group";
$rs = mysql_query($sql);
for($i = 0; $i <= mysql_num_rows($rs); $i++){
if($i != 0){//if we have to split results.
if(mysql_result($rs, $i, "group") != mysql_result($rs, $i - 1, "group")){
//if we are spliting groups:
echo ";";
}else{
echo ",";
};
};
echo "<a href="".mysql_result($rs, $i, "LinkTo")."">".mysql_result($rs, $i, "LinkText")."</a>";
};
echo '";';
?>strFdrop = strFdrop.split(";");
for(var i = 0; i < strFdrop.length; i++){
strFdrop[i] = strFdrop[i].split(",");
};
[/code]
×

Success!

Help @damon2003 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...