Jump to content

dynamic drop downs


digitalgod

Recommended Posts

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

[code]
<?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>
[/code]

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?
Link to comment
https://forums.phpfreaks.com/topic/17288-dynamic-drop-downs/
Share on other sites

Javascript ususally doesn't care if you include the semicolumn or not. The problem is probably with your use of shorthand opening tags ("[color=red][b]<?[/b][/color]"). The php within that block probably isn't parsed, as php doesn't accept it by default. Use "[b][color=red]<?php[/color][/b]" instead.
Link to comment
https://forums.phpfreaks.com/topic/17288-dynamic-drop-downs/#findComment-77506
Share on other sites

  • 3 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.