Jump to content

Archived

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

digitalgod

dynamic drop downs

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?

Share this post


Link to post
Share on other sites
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)">

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Whoa, I was just needing something like this too! :)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.