Problem in selecting second arraylist from mysql database

Hi…everyone :slight_smile:
Below i have attached my code. This code is working…but i want some modification in that code such that when i select ‘vegetable’ or ‘fruits’, it should give list of that selected one in second list, BUT THAT list should be stored in mysql database.
In the below program , i have hard-coded that second list, but i want second list to be dynamic from database.

Please, help me out.
Thank you for your help in advance :slight_smile:


<HTML>   
<HEAD>   
<SCRIPT LANGUAGE="JavaScript">   
<!--Begin JavaScript   
  
  
  
function setList(f) {   
 secondlist=eval("'"+f.elements['list1'].options[f.elements['list1'].selectedIndex].text+"'");   
 f.elements['list2'].options.length=0;   
 size=eval("lists['"+secondlist+"'].length");   
 for(i=0;i<size;i++) {   
  newval=eval("lists['"+secondlist+"'][i]");   
  f.elements['list2'].options[i]=new Option(newval);   
 };   
  f.elements['list2'].selectedIndex=0;   
};   
lists=new Array();   
lists['Fruit'] = new Array();   
  lists['Fruit'][0]="apple";   
  lists['Fruit'][1]="banana";   
  lists['Fruit'][2]="cantelope";   
  lists['Fruit'][3]="grape";   
  lists['Fruit'][4]="honeydew";   
  lists['Fruit'][5]="kiwi";   
  lists['Fruit'][6]="lemon";   
  lists['Fruit'][7]="lime";   
  lists['Fruit'][8]="mango";   
  
  
lists['Vegetables'] = new Array();   
  lists['Vegetables'][0]="carrot";   
  lists['Vegetables'][1]="celery";   
  lists['Vegetables'][2]="onion";   
  lists['Vegetables'][3]="potato";   
  lists['Vegetables'][4]="squash";   
  lists['Vegetables'][5]="tomato";   
  
  
//End JavaScript-->   
</SCRIPT>   
<BODY BGCOLOR=WHITE onLoad="setList(document.listsform)">   
  
<FORM NAME="listsform">   
<SELECT NAME="list1" onChange="setList(form)">   
<OPTION>Fruit</OPTION>   
<OPTION>Vegetables</OPTION>   
  
</SELECT>   
  
<SELECT NAME="list2">   
<OPTION>         </OPTION>   
<OPTION></OPTION>   
<OPTION></OPTION>   
<OPTION></OPTION>   
<OPTION></OPTION>   
<OPTION></OPTION>   
<OPTION></OPTION>   
<OPTION></OPTION>   
<OPTION></OPTION>   
  
</SELECT>   
</FORM>   
</BODY></HTML> 

You would need to use Ajax to achieve this, the best solution would be to use Ajax with JSON which will allow for easier data management for the return data.

Read this article for more information about Ajax and for the JSON library you would need the javascript source and the PHP source if your going to use PHP.

JSON JS: https://github.com/douglascrockford/JSON-js
JSON PHP: http://www.php.net/releases/5_2_0.php

Sorry posted the wrong link for JSON PHP http://php.net/manual/en/function.json-encode.php