Form Categories List

Discussion in 'Programming' started by Silver89, Jul 1, 2008.

  1. #1
    Hi,

    I'm making an advanced search form and I wish the users to be able to select a category frmo a static list which lists all the categories,

    When this category is selected a dynamic form list will appear showing all the subcategories in that category.

    There is a good example on: http://www.mininova.org/upload

    But how would I go about doing this?

    Thanks in advance :)
     
    Silver89, Jul 1, 2008 IP
  2. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #2
    That will be fairly simple. You need to create a DIV and populate its innerHTML property with new combo as per parent's selected item. Or hide all and show the one needed.

    Let me know if you need example.

    regards
     
    Vooler, Jul 1, 2008 IP
  3. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #3
    If you could provide a quick example that would be great, thanks.
     
    Silver89, Jul 1, 2008 IP
  4. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #4
    Are you using PHP as server-end language ? So that I can provide script that can generate required thingy dynamicaly.

    regards
     
    Vooler, Jul 1, 2008 IP
  5. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #5
    Yes mysql quries in php
     
    Silver89, Jul 1, 2008 IP
  6. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #6
    Ok, try this boy, save following code as html file on desktop and try:

    <script>
    	var sub_categories = new Array(
    		//categories under Animals
    		new Array('Cat', 'Dog', 'Elephant'), 		
    		
    		//categories under Strange Creatures
    		new Array('Mermaid', 'Unicorn', 'Medusa')
    	)
    	
    	function populate(main_id,combo_name) {
    		var arr = sub_categories[main_id-1];
    		var htm = "<select name='"+combo_name+"'>";
    		for(var i=0; i<arr.length; i++) {
    			htm = htm + "<option>" + arr[i] + "</option>";
    		}
    		document.getElementById('subdiv').innerHTML = htm;
    	}
    </script>
    <table border="1">
    <tr>
    	<td>
    		<select name=main id=main 
    			onChange="javascript:populate(this.value, 'cat_id')">
    
    			<option value='0'>Select Category</option>
    			<option value='1'>Animals</option>
    			<option value='2'>Strange Creatures</option>
    		</select>
    	</td>
    	<td>
    		<div id="subdiv">None selected</div>
    	</td>
    </tr>
    </table>
    Code (markup):
    I hope it helps.

    regards
     
    Vooler, Jul 1, 2008 IP
  7. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #7
    Silver89, Jul 1, 2008 IP
  8. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #8
    Rightnow, moving to bed :), really tired, will provide you full php based example when I wake up (for sure).

    regards
     
    Vooler, Jul 1, 2008 IP
    Silver89 likes this.
  9. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #9
    I managed to work it out, many thanks!
     
    Silver89, Jul 1, 2008 IP