Jump to content


Photo

dynamic drop downs


  • Please log in to reply
4 replies to this topic

#1 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 11 August 2006 - 10:01 PM

hey guys,

I'm trying to make 2 dynamic drop downs where the 2nd one is populated with elements from a db depending on what was selected in the 1st drop down.

The first drop down is populated with an array, it contains the days of the week. So when you choose a day I want the second drop down to be populated with the names of clubs that are opened on that day.

something like SELECT club FROM clubnights WHERE club = "monday"

this is what I have so far (got it from nogray's post somewhere on this forum

<?php
$query = "SELECT * FROM clubs";
$result = mysql_query ($query);

while ($row = mysql_fetch_array($result)) {
    $query1 = "SELECT * FROM clubnights WHERE club = '".$row['name']."'";
	$result1 = mysql_query ($query1);
    
    if ($row['name'] !='') {
		$sub_arrays .= "subs[".$row['name']."] = new Array(";
    
    	while ($row1 = mysql_fetch_array($result1)) {
        	$sub_arrays .= "\"".$row1['club']."\",";
    	}
    	$sub_arrays = trim($sub_arrays, ",") .");\n";
	}

}

list($y,$m,$d) = explode("-",date("Y-m-d"));
$days = array();

for ($i=0;$i<=6;$i++) {
     array_push($days,date("l", mktime(0,0,0,$m,$d+$i,$y)));
}

?>

<script language="javascript">
var subs = new Array();

<?= $sub_arrays ?>

function fill_subs(val){
    if (val != ""){
        var sub_sel = document.getElementById('club');
		
        sub_sel.innerHTML="";
        opt = document.createElement("OPTION");
        opt.value = "";
        opt.innerHTML = "Select";
        sub_sel.appendChild(opt);
        
        for(i=0; i<subs[val].length; i++){
            opt = document.createElement("OPTION");
            opt.value = subs[val][i];
            opt.innerHTML = subs[val][i];
            
            sub_sel.appendChild(opt);
        }
    }

}
</script>

//......

<form name="list" method="post" action="index.php?a=list">
		<input type="hidden" name="process_a" value="yes"/>
		<?
		echo '<select name="night" onChange="fill_subs(this.value);"><option value="">Select one</option>';
		for($i=0;$i<count($days);$i++) { 
			echo  '<option value='.strtolower($days[$i]).'>'.$days[$i].'</option>';
		}
		echo "</select>";

		echo '<select id="club" name="club"><option value="">Select one</option>';
		echo "</select>";
		?>
        </form>

it keeps telling me that I have an error on line 200 which is the onChange event : <select name="night" onChange="fill_subs(this.value);">

any ideas what I'm doing wrong?


#2 may

may
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 19 August 2006 - 07:58 PM

Hello!

I guess u need to get rid of the semi colon after the functions name.

Try this:

<select name="night" onChange="fill_subs(this.value)">



#3 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 20 August 2006 - 09:11 AM

Javascript ususally doesn't care if you include the semicolumn or not. The problem is probably with your use of shorthand opening tags ("<?"). The php within that block probably isn't parsed, as php doesn't accept it by default. Use "<?php" instead.

#4 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 22 August 2006 - 02:46 AM

Whoa, I was just needing something like this too! :)

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#5 ExosSho

ExosSho
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 08 September 2006 - 09:30 AM

i dont know if u solved the problem, but maybe the mysql part isnt right,
i think it should be while($row = mysql_fetch_assoc($result))
insted of mysql_fetch_array.
but maybe im wrong




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users