|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi
I found this javascript which creates a dependendant dropdown lists. That is if you select an item from the one dropdown list the other dropdown lists updates with the option relating to that item. An example : if u have 2 dropdownlist (lets call drop1 country and drop2 city) if you select a country from country dropdown the city dropdown updates with the available cities for that country. So far so good. Now what i wanted to do is populate the country dropdown from a mysql database using php , which worked. here is the javascript i found on the internet: <script> // Define the values for your drop downs here // Note: If you want different text than value you can also specify text1_0 etc. value1_0='#'; value1_1='1'; value1_2='2'; value1_3='3'; text1_0='Pick a city'; text1_1='Copenhagen'; text1_2='Odense'; text1_3='Skagen'; value2_0='#'; text2_0='Pick a city'; value2_1='1'; value2_2='2'; value2_3='3'; text2_1='Stockholm'; text2_2='Gothenburg'; text2_3='Kiruna'; // Continue with value3_0 etc... // Leave the rest of the script as is function update_dropdowns(mother) { child=0; document.myform.child.options.length=1; while(eval('window.value'+mother+'_'+child)||eval( 'window.text'+mother+'_'+c hild)){ document.myform.child.options.length=child+1; id=mother+'_'+child; if (!eval('window.value'+id)) {eval('value'+mother+'_'+child+'=text'+id)} if (!eval('window.text'+id)) {eval('text'+mother+'=value'+id)} document.myform.child.options[child].value=eval('value'+id); document.myform.child.options[child].text=eval('text'+id); child++; } } </script> Now i want this part not to be hardcoded , instead pulled from a Mysql database using php: // Define the values for your drop downs here // Note: If you want different text than value you can also specify text1_0 etc. value1_0='#'; value1_1='1'; value1_2='2'; value1_3='3'; text1_0='Pick a city'; text1_1='Copenhagen'; text1_2='Odense'; text1_3='Skagen'; value2_0='#'; text2_0='Pick a city'; value2_1='1'; value2_2='2'; value2_3='3'; text2_1='Stockholm'; text2_2='Gothenburg'; text2_3='Kiruna'; // Continue with value3_0 etc... This is what i tried but didn`t work , can anyone look at it and help to find the problem , ie find out why the child dropdown doesn`t update properly as it worked with the hardcoding of their text & values: I put this instead of the above: PHP Code:
Thanks for any further help |
|
#2
|
||||
|
||||
|
What's the output of your code? Does it print the javascript correctly? One option, incidentally, might be to skip the javascript and have the county box submit the form on change and populate the city box based on its value. Of course, you'd have to remember and fill in all other form values as well on the reload, but that's not too big a deal.
|
|
#3
|
|||
|
|||
|
Yes, you do need to post your output in order for someone to help you.
__________________
__________________________________________________ _ Wil Moore III, MCP | Integrations Specialist | Senior Consultant Are You Listed...? | DigitallySmooth Inc. |
|
#4
|
|||
|
|||
|
Kupilot, have you read the forum rules?
http://www.devarticles.com/forum/an...hp?s=&forumid=4 Please see section (3) |
|
#5
|
|||
|
|||
|
laidbak , i`ve read the rules , sorry that you think that i`m waisting your time , i`ve attached both the code and the IE output of it , if you think you can help , then please do , i think my question should be simple to answer , as i`m looking for a way to populate a javascript array with data from mysql database using php , i`m trying to solve it myself , but can`t find where the problem is , there is no error from IE , just that the second dropdown box is not correctly & dynamiclly populated.
Hope after revising my post someone would help , and not teach the rule on how to post ![]() |
|
#6
|
|||
|
|||
|
You are producing an invalid set of data:
I took your variable set and exported it to a text file, then imported it into excel to sort and get a clear view of the results... Here are the first couple results: Code:
text1 0='Pick A City'; text1 5='98'; text2 0='Pick A City'; text2 0='Pick A City'; text2 6='Internet Explorer'; text2 7='Outlook Express'; value1 0='Pick A City'; value1 5='5'; value2 0='Pick A City'; value2 0='Pick A City'; value2 6='6'; value2 7='7'; Although you do have a valid set of data, you are missing one fundamental part of your application. You are going from text1_0 to text1_5, and the same for the value match for this variable. If you take the html your application produced and put it into its own html file, try changing this: value1_0='Pick A City'; text1_0='Pick A City'; value1_5='5'; text1_5='98'; To this: value1_0='Pick A City'; text1_0='Pick A City'; value1_1='5'; text1_1='98'; for clarity you can even add these lines: value1_2='6'; text1_2='2000'; See that it works. In summary, all you need to do is make sure your query results produce a proper sequencial set of indexes. |
|
#7
|
|||
|
|||
|
Hi laidbak
Thanks for the reply. So what you`re saying the problem is with the logic of the query or the sort of the out. What it should output is like this: I have 2 tables in the database , table1 is categories with catid/name fields , table 2 is subcategories with subcatid/subname/catid fields. They are joind by catid fields.(as you can see in the query). Now the javascript array should look like this: value1_0='Pick A Country'; value1_1='Bangkok'; value1-2='singapore'; text1_0='Pick A country'; text1_1="Bangkok'; text1_2="singapore'; and so on for category 1 as the idea is that the value and text format is as follows: valuecatid_subcatid=string in the database field; textcatid_subcatid=string in the database field; The do/while loop tries to do that for me as you may see in the code, but i think the sorting of the array needs to be refind. No who would you do this task , I don`t know how to. I tried to lookup in the PHP/Mysql manuals and all over the forums in the internet , couldn`t find a solution. I`d like to know that because i want also to write a dynamic news ticker , where the news articles are pulled from mysql database , and put in an javascript array like this: msg[0]=msg1; msg[1]=msg2; and so on. Shall I change the do/while loop in a for loop, or shall i put it inside a for loop , and how would i do that , especially if you this category/subcategory relation. Thanks in advance. |
|
#8
|
||||
|
||||
|
Couldn't you just change these lines:
PHP Code:
to PHP Code:
or something similar so that you get sequential values? |
|
#9
|
|||
|
|||
|
Quote:
Sorry dhouston that didn`t help. There must be a different way to populate any javascript array (assoc/multidimensional or whatever) from mysql database using php. I`m still trying to figure it out though. |
|
#10
|
||||
|
||||
|
Try something like the following code.
PHP Code:
The output of this when I run it is as follows: Code:
value1_0='6'; text1_0='Sri Lanka'; value1_1='8'; text1_1='Bangladesh'; value1_2='9'; text1_2='China'; value2_0='1'; text2_0='Word'; value2_1='3'; text2_1='Power Point'; value2_2='23'; text2_2='Internet Explorer'; If that's not exactly what you need, you can see the mechanism I've used to generate that output and should be able to adapt it according to your needs. Hope this helps. Last edited by dhouston : July 18th, 2003 at 02:54 PM. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > dependant dropdown lists , please help find the problem! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|