Jump to content


Photo

drop down with db


  • Please log in to reply
13 replies to this topic

#1 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 25 July 2006 - 09:19 PM

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

<?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);
}

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

#2 thepip3r

thepip3r
  • Members
  • PipPipPip
  • Advanced Member
  • 289 posts

Posted 25 July 2006 - 09:49 PM

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:

or die(query_error());

it should be:

or die(mysql_error());



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...

<?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'].'", "");';
}
?>

should look like:

<?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'].'", "");';
}
?>


#3 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 25 July 2006 - 09:52 PM

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

#4 thepip3r

thepip3r
  • Members
  • PipPipPip
  • Advanced Member
  • 289 posts

Posted 25 July 2006 - 09:57 PM

FYI... you can also abbreviate your arraypush() to:

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


#5 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 25 July 2006 - 10:09 PM

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...

<?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'].'");
			}';
}?>

still can't find why though

#6 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 25 July 2006 - 10:27 PM

you have to check the selectedIndex for drop down lists
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#7 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 25 July 2006 - 10:37 PM

you have to check the selectedIndex for drop down lists


how do I do that?

#8 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 25 July 2006 - 10:43 PM

try
echo 'if (document.form.club.options[document.form.club.selectedIndex].value == '.$clubList[$].'){

adopted from http://www.pageresou...pt/jdropbox.htm
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#9 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 25 July 2006 - 10:50 PM

still not working...

#10 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 25 July 2006 - 11:03 PM

need a space before ORDER, and add quotes too:
$sql_nights='SELECT * FROM clubnights WHERE club = "'.$clubList[$i].'" ORDER BY night';

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

Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#11 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 25 July 2006 - 11:15 PM

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

<?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'].'");
			}';
}
?>

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




#12 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 25 July 2006 - 11:24 PM

do you have document.form.night in your document? For the subcat thing... maybe you want to loop through the records??
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#13 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 25 July 2006 - 11:29 PM

yes I do have document.form.night

<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>

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

#14 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 26 July 2006 - 05:14 PM

anyone?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users