473,698 Members | 2,339 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need Help with Dynamic Drop Down Box

1 New Member
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 2803
dlite922
1,584 Recognized Expert Top Contributor
@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='sele cted'" 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
phpnewbie26
52 New Member
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
unauthorized
81 New Member
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
7013
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 changes box1, I want to set the selected index in box2 = 0. When I do this, I dont get an error, but when the page loads, it still has the selected value and not 0. It seems that it is getting the selected value from the viewstate
1
4292
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 encounters a server control, it closes itself.
3
3048
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 DropDownHelper(menuArray, top, left, height) { var currItem = new String(); var item; var idStr;
2
1981
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 not properly readjusting the width of a dynamically produced drop down box, and some browsers having problems adjusting the height and widths (most notably NN). Most solutions involve manually refreshing the browser window and/or padding the...
4
9821
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 user selects. So far, it doesn't seem to be firing. I am only testing the functionality because I have quite a bit of text to display depending on the users input and want to get it working first before I bother typing everything.
1
1563
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) multiplied by a weighted score. The final column for each employee would be the calculated score. I know how to accomplish this with vbscript but I need it to process on the screen so the user can see the calculated score as they make selections....
6
8301
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 information I want by doing a Response.write. I am having problems dynamically creating a drop down list from the data in the recordset. The drop down is created but it is empty. Any help would be greatly appreciated. A sample of the code: <% 'On...
2
1784
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 onChange event to try and capture the users selection but it always passes the last entry in the drop-down list not what the user selects. Can anyone help me with this. I am very new to ASP\Jscript\Vbscript. A snippent of the code is below. ...
2
2905
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 value from the list and it passes that value to the function to get the appropriate data and it prints out the values (which it does just fine). The thing is when I select an item from the drop-down list the other html disappears (even the...
0
8674
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
8603
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
9157
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
7725
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...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
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
3046
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
2
2329
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
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.