473,386 Members | 1,708 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,386 software developers and data experts.

Need Help with Dynamic Drop Down Box

Hi,

I have 2 questions. First, I am building a form with multiple drop down boxes. The options of the second drop down box will depend on what the user chooses on the first, options of the third drop down box depend on what they choose on the second, etc. The values of each option are inside a database which I can query for and put into a php array. I am wondering what is the easiest way to build this.

Second, when this form is completed, it should be viewable/editable by the user so it should be able to show the drop down menu with the selected options that the user chose. How would I be able to set the certain options for the drop down box to what the user chose and be able to make it dynamic again so they can make changes to what they chose.

Thanks for your help!
May 5 '09 #1
3 2791
dlite922
1,584 Expert 1GB
@happyman992
Welcome to bytes happyman992,

Since PHP is serverside, the page must be refreshed to let PHP know what was selected in the parent dropdown box to fill the child (Unless you like to learn AJAX).

use the select box's onchange() method to submit the form when they select the first option. Use that to populate the second, do the same after they select an option from the second drop down to populate the third.

To show what was selected, when you populate the dropdown options insert an if statement in the loop to see if it's equal to the variable it was selected before the refresh, if it is include the "selected='selected'" attribute of the <options> tag. If it's not equal, then skip just display the option.

If you get stuck at any detail, come back to post your question.

Googling for: "PHP populating multiple select" (without the quotes) gives you some pretty good example of other people trying to do this and perhaps some tutorials and code for you to look at.

Best of luck,




Dan
May 6 '09 #2
Hi!

I've been looking around trying to figure out how onchange() and functions related to it works. Is it possible that you show an example of it being used. My problem is generating a parent child drop down menu both populated from an array and not a database. Thanks in advance.
Jun 3 '09 #3
Do you mean this:
Expand|Select|Wrap|Line Numbers
  1. $data = array("item1", "item2", "item3");
  2.  
  3. echo "<select>";
  4. foreach($data as $item) echo "<option>$item</option>";
  5. echo "</select>";
There is nothing difficult about it - it's a simple string concatenation. Am I missing something?

As for onchange, it's an event that you can respond by calling a javascript function.
Expand|Select|Wrap|Line Numbers
  1. <form name="form1">
  2. <select name="select1" onchange="javascript: OnSelect1Changed()">
  3. <!-- options go here --/>
  4. </select></form>
You can then use this JS code:
Expand|Select|Wrap|Line Numbers
  1. function OnSelect1Changed() {
  2. var selected = document.forms.form1.select1.selectedIndex;
  3. // TODO: react on the selected item somehow
  4. }
Dig up google if there is something you don't understand.
Jun 3 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Robin Bonin | last post by:
In my user contol I am creating a set of dropdownlists. Each list is created based on input from the other lists. The problem I am having is setting the selected index on the lists. If someone...
1
by: tribal boy | last post by:
Guys, I am using a dynamic menu which uses xml,xsl a css file and javascript. This works fine when there are no server controls around or underneath it. The problem is whenever the menu...
3
by: scaredemz | last post by:
hi, so i'm creating a dynamic drop-down menu. the menu and the text show up fine in IE but only the drop-down shows in Firefox without the menu text. Below is the fxn code. help pls. function...
2
by: taras.di | last post by:
Hi everyone, I've been reading up on how to create a drop down box who's context is dynamically produced based on the value of a previous select box. I've read a lot about some of the browsers...
4
by: tjonsek | last post by:
I have two drop down boxes on a form. One feeds the second a list options based on user selection. With the second drop down, I want code that displays dynamic text in a <div> based on whatever the...
1
by: pmarisole | last post by:
I need help in calculating a score from a row of drop-down values. I need to use the onChange to tally the score as the user moves across 9 categories (with drop-down selection of 1-9 or N/A)...
6
by: mcgrew.michael | last post by:
I hope this is the right group. I am very new to ASP so this is probably a stupid question. I have some vbscript that query's AD and populates a recordset. I know the recorset contains the...
2
by: Michael McGrew | last post by:
I have a Dynamic drop-down box that is populated based on a ADO query. This works fine. I want to capture the users selection and assign it to a variable for use in another query. I am using the...
2
by: chobo | last post by:
Hi, I am trying to make a dynamic menu using javascript and an xml file. The structure of the page (test.html) is in the code below. When I load up the page the drop-down list is there, so I select a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
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...

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.