Jump to content

Archived

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

digitalgod

drop down with db

Recommended Posts

hey guys,

I've been pulling my hair out trying to figure this out...

I have 2 drop downs, first 1 is populated from a db, second one displays the sub categories of what has been selected in the first drop down

here's what I got so far

[code]
<?php
$sql_clubs='SELECT * FROM clubs ORDER BY name';
$req_clubs=mysql_query($sql_clubs) or die(query_error());
$numRows = mysql_num_rows($req_clubs);
$clubList = array();
?>
<script type="text/javascript">
function fillclub(){
// this function is used to fill the club list on load
<?php
while($data = mysql_fetch_assoc($req)) {
array_push($clubList,$data['name']);
echo 'addOption(document.form.club, "'.$data['name'].'", "'.$data['name'].'", "");';
}
?>
}

function Selectnight(){
// ON selection of club this function will work

removeAllOptions(document.form.night);
addOption(document.form.night, "", "Choose one", "");

<?php
for ($i=0;$i<=$numRows;$i++) {
$sql_nights='SELECT * FROM clubnights WHERE club = '.$clubList[$i].'ORDER BY night';
$req_nights=mysql_query($sql_nights) or die(query_error());
$night_data = mysql_fetch_array($req_nights);
echo 'if(document.form.club.value == '.$clubList[$i].'){
addOption(document.form.night,"'.$night_data['night'].'", "'.$night_data['night'].'");
}';
}
?>

function removeAllOptions(selectbox)
{
var i;
for(i=selectbox.options.length-1;i>=0;i--)
{
//selectbox.options.remove(i);
selectbox.remove(i);
}
}


function addOption(selectbox, value, text )
{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;

selectbox.options.add(optn);
}
[/code]

but it's not working... any ideas what's wrong?

Share this post


Link to post
Share on other sites
this would be more of a javascript question than a PHP one; but, considering you don't have any <html> or <head> tags anywhere (where your javascript functions MAY need to go) that would be the first place i'd look.

edit:

AND this is not correct:

[code]or die(query_error());[/code]

it should be:

[code]or die(mysql_error());[/code]



edit 2:

unless you have $req called somewhere else in the page that you didn't copy and paste, it looks like your loop should be mysql_fetch_assoc'ing ($req_clubs) instead of just $req...

[code]<?php
$sql_clubs='SELECT * FROM clubs ORDER BY name';
$req_clubs=mysql_query($sql_clubs) or die(query_error());
$numRows = mysql_num_rows($req_clubs);
$clubList = array();
?>
<script type="text/javascript">
function fillclub(){
// this function is used to fill the club list on load
<?php
while($data = mysql_fetch_assoc($req)) {
array_push($clubList,$data['name']);
echo 'addOption(document.form.club, "'.$data['name'].'", "'.$data['name'].'", "");';
}
?>[/code]

should look like:

[code]<?php
$sql_clubs='SELECT * FROM clubs ORDER BY name';
$req_clubs=mysql_query($sql_clubs) or die(query_error());
$numRows = mysql_num_rows($req_clubs);
$clubList = array();
?>
<script type="text/javascript">
function fillclub(){
// this function is used to fill the club list on load
<?php
while($data = mysql_fetch_assoc($req_clubs)) {
array_push($clubList,$data['name']);
echo 'addOption(document.form.club, "'.$data['name'].'", "'.$data['name'].'", "");';
}
?>[/code]

Share this post


Link to post
Share on other sites
well I only placed the part that I know has errors in, the form itself is just 2 drop downs and a submit button... first drop down has a onChange="fillnight();"

I think it's php related because I'm positive I'm doing something wrong in php, it works fine if I use predefined values and only javascript

Share this post


Link to post
Share on other sites
FYI... you can also abbreviate your arraypush() to:

[code]$clubList[] = $data['name'];[/code]

Share this post


Link to post
Share on other sites
thanks thepip3r but it's still not working...

*edit*

I know it's this part that doesn't work because if I take it off the page is displayed...

[code]
<?php
for ($i=0;$i<=$numRows;$i++) {
$sql_nights='SELECT * FROM clubnights WHERE club = '.$clubList[$i].'ORDER BY night';
$req_nights=mysql_query($sql_nights) or die(query_error());
$night_data = mysql_fetch_assoc($req_nights);
echo 'if(document.form.club.value == '.$clubList[$i].'){
addOption(document.form.night,"'.$night_data['night'].'", "'.$night_data['night'].'");
}';
}?>
[/code]

still can't find why though

Share this post


Link to post
Share on other sites
[quote author=ryanlwh link=topic=101844.msg403483#msg403483 date=1153866452]
you have to check the selectedIndex for drop down lists
[/quote]

how do I do that?

Share this post


Link to post
Share on other sites
try
[code]echo 'if (document.form.club.options[document.form.club.selectedIndex].value == '.$clubList[$].'){ [/code]

adopted from http://www.pageresource.com/jscript/jdropbox.htm

Share this post


Link to post
Share on other sites
need a space before ORDER, and add quotes too:
[code]$sql_nights='SELECT * FROM clubnights WHERE club = "'.$clubList[$i].'" ORDER BY night';[/code]

Now if $clubList contains strings:
[code]echo 'if (document.form.club.options[document.form.club.selectedIndex].value == "'.$clubList[$].'"){ [/code]

Share this post


Link to post
Share on other sites
I think we're getting somewhere, the page is displayed but there's nothing in the drop downs, it's giving me a javascript error saying that there's something wrong here

selectbox.options.add(optn);

what's wrong now? :(
by the way is that part any good

[code]
<?php
for ($i=0;$i<=$numRows;$i++) {
$sql_nights='SELECT * FROM mtlguest_clubnights WHERE club = "'.$clubList[$i].'" ORDER BY night';
$req_nights=mysql_query($sql_nights) or die(query_error());
$night_data = mysql_fetch_assoc($req_nights);
echo 'if (document.form.club.options[document.form.club.selectedIndex].value == "'.$clubList[$i].'"){ 
addOption(document.form.night,"'.$night_data['night'].'", "'.$night_data['night'].'");
}';
}
?>[/code]

I have a feeling that it will only display 1 sub category...


Share this post


Link to post
Share on other sites
do you have document.form.night in your document? For the subcat thing... maybe you want to loop through the records??

Share this post


Link to post
Share on other sites
yes I do have document.form.night

[code]
<div id="article">
<form name="form" id="form" action="admin.php?a=delclubnight" method="post">
<input type="hidden" name="process" value="yes" />
    <table border="0">
      <tr>
        <td>Club Name:</td>
        <td><select name="club" id="club" onChange="Selectnight();">
          <option>Choose one</option>       
        </select></td>
      </tr>
      <tr>
        <td>Night::</td>
        <td><select name="night" id="night">
          <option>Choose one</option>
        </select></td>
      </tr>
    </table>
<br />
<input type="submit" value="Delete Night" />
</form>
</div>
[/code]

but it's the first drop down that isn't displaying anything anymore, If I remove the part of code that populates the subcat the first drop down shows me the list of clubs available in the database

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.