Combo Boxes and sub-combo boxes Help

Discussion in 'PHP' started by linkinpark2014, Sep 15, 2008.

  1. #1
    Hi!

    I have a combo box 1 with a list of choices
    let's say:

    Blue
    Green
    Red
    Purple

    are my choices

    What I want to happen is when I choose a certain choice in combo box 1, have another combo box 2 appear with more choices.

    Example, when I choose Blue in combo box 1, automatically another combo box 2 would appear with a list of choices.

    BUT I don't want the combo box 2 showing on the form AT ALL if I chosed Red as my choice in combo box 1. Meaning, Blue would be the only choice in combo box 1 that has other subcategories and combo box 2 only appears when Blue is chosen otherwise it stays hidden.
    and so on when I choose Red, Purple..each 1 has its own new combo box


    so guyz do u hav any Idea?
    I heard everything should implemented in java script
     
    linkinpark2014, Sep 15, 2008 IP
  2. algaidaman

    algaidaman Active Member

    Messages:
    106
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #2
    You may want to consider either Javascript or AJAX for this since PHP executes serverside when the page loads. Javascript and AJAX can be called anytime since they execute clientside.

    For you it may be easier to use Javascript though.
     
    algaidaman, Sep 16, 2008 IP
  3. Hade

    Hade Active Member

    Messages:
    701
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    90
    #3
    I've done it with javascript.

    I have a first combo box (eg. regions), and create lots of second comboboxes with a PHP loop (eg. stores).

    Here is the javascript code which shows the relevant second combo box:


    		function showhide(sel){
    			
    		//alert(category);
    
    			for(var i = 0; i < sel.options.length; ++i){
    			//alert('d');
    			//alert(sel.options[i].value);
    				if (sel.options[i].value != 0) document.getElementById('store_' + sel.options[i].value).style.display = 'none';
    			}
    			
    			var category = sel.options[sel.selectedIndex].value;
    			if (category !=0){
    				document.getElementById('store_' + category).style.display = 'inline';
    			}
    
    			enablebutton();
    		
    		}
    Code (markup):



    				<select style="width:180px;"  name="region" id="selregion" onchange="showhide(this)">
    					<option value="0">Select a Region</option>
    						<?php
    							
    						
    							for ($l=0; $l < count($regions); $l++ ){
    								echo '<option value="'. $regions[$l]->id .'">' . $regions[$l]->title ."</option>\n";
    							}
    						
    						?>
    				</select>
    
    
    
    
    				<?php
    				
    				for ($l=0; $l < count($stores); $l++ ){
    					?><select style="width:180px; display:none"  name="store_<?php echo $stores[$l]->id;?>" id="store_<?php echo $stores[$l]->id;?>">
    						<option value="0">Select a Store</option>
    						<?php
    						for ($s=0; $s < count($stores); $s++ ){
    							echo '<option value="' . $stores->id . '" '.  (($stores->user_id == $user->get('id'))?'selected="selected" ':' ')   .'>' . $stores->name . '</option>';	  
    						}
    						?>
    					</select><?php
    				}
    				?>
    
    
    
    PHP:

    The above code was pulled from a Joomla site so will need tweaking, but you get the general idea.
     
    Hade, Sep 16, 2008 IP
  4. linkinpark2014

    linkinpark2014 Peon

    Messages:
    153
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanx all for ur reply..
    but my prob solved from yesterday..
    here is my code snippets:

     
    linkinpark2014, Sep 16, 2008 IP
  5. algaidaman

    algaidaman Active Member

    Messages:
    106
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #5
    That's good to hear. Those select/option dropdowns can be tricky when it comes to cross browser scripting.
     
    algaidaman, Oct 1, 2008 IP