12 Replies - 2858 Views - Last Post: 26 November 2007 - 06:11 AM Rate Topic: -----

#1 didgy58  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 246
  • Joined: 23-October 07

drop down menus

Posted 23 November 2007 - 04:37 AM

hi guys and gals im kinda getting stuck on this little problem, i have four select boxes, the 1st im hard coding with all the manufacturer names the next ones i want to dynamically load depending on the 1st selection, so when i say select apple a query will be done on the table and return all the named apple products in the next drop down, im only using one database for this thought, is this a stupid idea??? or should i use multiple tables?? thanks

dan.

i have code which i have got hold of from the internet but i dont seem to be able to make much sense of it all well i sort of do and dont.

<?php
//***************************************
// This is downloaded from www.plus2net.com //
/// You can distribute this code with the link to www.plus2net.com ///
//  Please don't  remove the link to www.plus2net.com ///
// This is for your learning only not for commercial use. ///////
//The author is not responsible for any type of loss or problem or damage on using this script.//
/// You can use it at your own risk. /////
//*****************************************

$dbservertype='mysql';
$servername='localhost:3306';
// username and password to log onto db server
$dbusername='root';
$dbpassword='';
// name of database
$dbname='danssite';

////////////////////////////////////////
////// DONOT EDIT BELOW  /////////
///////////////////////////////////////
connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
//////// End of connecting to database ////////
?>

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<title>Multiple drop down list box from plus2net</title>
<script language=Javascript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='dd.php?cat=' + val;
}

</script>
</head>

<body>
<?php

/*
If register_global is off in your server then after reloading of the page to get the value of cat from query string we have to take special care.
To read more on register_global visit.
  http://www.plus2net.com/php_tutorial/register-globals.php
*/
$cat=$_GET['cat']; // Use this line or below line if register_global is off
//$cat=$HTTP_GET_VARS['cat']; // Use this line or above line if register_global is off

///////// Getting the data from Mysql table for first list box//////////
$quer2=mysql_query("SELECT Manufacturer FROM mp3players where Manufacturer='apple'"); 
///////////// End of query for first list box////////////

/////// for second drop down list we will check if category is selected else we will display all the subcategory///// 
if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT Capacity  FROM mp3players where Manufacturer='apple'"); 
}else{$quer=mysql_query("SELECT Capacity  FROM mp3players where Manufacturer='apple'"); } 
////////// end of query for second subcategory drop down list box ///////////////////////////

echo "<form method=post name=f1 action='dd-check.php'>";
/// Add your form processing page address to action in above line. Example  action=dd-check.php////
//////////		Starting of first drop downlist /////////
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
while($noticia2 = mysql_fetch_array($quer2)) { 
if($noticia2['cat_id=']==$cat){echo "<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"."<BR>";}
else{echo  "<option value='$noticia2[cat_id]'>$noticia2[category]</option>";}
}
echo "</select>";
//////////////////  This will end the first drop down list ///////////

//////////		Starting of second drop downlist /////////
echo "<select name='subcat'><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer)) { 
echo  "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";
}
echo "</select>";
//////////////////  This will end the second drop down list ///////////
//// Add your other form fields as needed here/////
echo "<input type=submit value=Submit>";
echo "</form>";
?>
<center><a href='http://www.plus2net.com'>PHP SQL HTML free tutorials and scripts</a></center> 
</body>

</html>



when i use this as im just testing and learning it at the moment the drop down boxes just turn up empty on the screen

the database is in danssite and the database name = mp3players

the fields im using are Manufacturer, Name for the 2 drop downs above but i cant seem to get the drop downs to display anything at all.

there must be a connection as the boxes appear?? am i correct in assuming that otherwise a statment would appear saying no connection??

thanks

dan

Is This A Good Question/Topic? 0
  • +

Replies To: drop down menus

#2 cloudy_day  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 05-July 07

Re: drop down menus

Posted 23 November 2007 - 05:05 AM

So when the user selects a manufacturer from the first select object the page refreshes and the options in the second select are set according to the chosen manufacturer from the first select? Just getting it straight so I can help.
Was This Post Helpful? 0
  • +
  • -

#3 didgy58  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 246
  • Joined: 23-October 07

Re: drop down menus

Posted 23 November 2007 - 05:24 AM

yeah welli thats correct in the example code i have given above it uses a dynamic list for the first box as well but i dont want to do that, i want to hard code the values for the manufacturers in and then dynamically link the rest, any help would be much appreciated

thanks
Was This Post Helpful? 0
  • +
  • -

#4 nav33n  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 71
  • Joined: 02-October 07

Re: drop down menus

Posted 23 November 2007 - 06:47 AM

You can fetch the values from the table, populate the first select box. Then you can use onchange javascript event(in the first select box) TO submit the form and store the value of the first select box in another variable, then use that variable as a condition for your second select box.
Was This Post Helpful? 0
  • +
  • -

#5 didgy58  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 246
  • Joined: 23-October 07

Re: drop down menus

Posted 23 November 2007 - 06:55 AM

nav33 thanks a million that was so stupid of me after uve said that it clicked, the only problem i now have is im using 4 boxes i can get the first 2 to work but when i try to pass onto the third i get a problem

the 2nd choice has a space into it and i can see it in the url as cat=ipod%20classic

so i guess thats why its not working because it couldnt find that in the databse so the third box wont fill am i right in assuming this??
Was This Post Helpful? 0
  • +
  • -

#6 didgy58  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 246
  • Joined: 23-October 07

Re: drop down menus

Posted 23 November 2007 - 07:11 AM

actually just been shown a little debug method and ive figured out the problem is due to the fact that the second choice isnt goin to the right category bit not sure if you understand

<?php

$dbservertype='mysql';
$servername='localhost:3306';
// username and password to log onto db server
$dbusername='root';
$dbpassword='';
// name of database
$dbname='danssite';


connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
//////// End of connecting to database ////////
?>

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<title>Multiple drop down list box from plus2net</title>
<script language=Javascript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
var val2=form.subcat.options[form.subcat.options.selectedIndex].value;
var val3=form.subcat1.options[form.subcat1.options.selectedIndex].value;
self.location='dd.php?cat=' + val + val2 + val3;
}
</script>
</head>
<body>

<?php

$test=$_GET['test'];
$cat=$_GET['cat']; 
$subcat=$_GET['subcat'];
$subcat1=$_GET['subcat1'];

print "<br> TEST= ".$test;
print "<br> cat= ".$cat;
print "<br> subcat= ".$subcat;
print "<br> subcat1= ".$subcat1;

///////// Getting the data from Mysql table for first list box//////////
$quer=mysql_query("SELECT Manufacturer FROM mp3players"); 
///////////// End of query for first list box////////////

/////// for second drop down list we will check if category is selected else we will display all the subcategory///// 
if(isset($cat) and strlen($cat) > 0){
$quer2=mysql_query("SELECT Name  FROM mp3players where Manufacturer = '$cat'"); 
}else{$quer2=mysql_query("SELECT Name  FROM mp3players where Manufacturer= '$cat'"); } 
////////// end of query for second subcategory drop down list box ///////////////////////////

///// third drop down box query again check if the category has been selected esle we will display all the subcategory////

if(isset($subcat) and strlen($subcat) > 0){
$quer3=mysql_query("SELECT Capacity  FROM mp3players where Name = '$subcat'"); 
}else{$quer3=mysql_query("SELECT Capacity  FROM mp3players where Name= '$subcat'"); } 

//end of form////

echo "<form method=post name=f1 action='dd-check.php'>";






//////////		Starting of first drop downlist /////////

echo "<select name='cat' onchange=\"reload(this.form)  \"><option value=''>Select one</option>";
while($noticia2 = mysql_fetch_array($quer)) { 
if($noticia2['Manufacturer=']==$cat){
		echo "<option selected value='$noticia2[Manufacturer]'>$noticia2[Manufacturer]</option>"."<BR>";}
	else{
		echo  "<option value='$noticia2[Manufacaturer]'>$noticia2[Manufacturer]</option>";}
}
echo "</select>";
//////////////////  This will end the first drop down list ///////////

//////////		Starting of second drop downlist /////////

echo "<select name='subcat' onchange=\"reload(this.form)  \"><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer2)) { 
if($noticia['Name=']==$subcat){
		echo "<option selected value='$noticia[Name]'>$noticia[Name]</option>"."<BR>";}
	else{
		echo  "<option value='$noticia[Name]'>$noticia[Name]</option>";}
}
echo "</select>";
//////////////////  This will end the second drop down list ///////////
//// starting third drop down list//////

echo "<select name='subcat1'><option value=''>Select one</option>";
while($noticia3 = mysql_fetch_array($quer3)) { 
echo  "<option value='$noticia3[Capacity]'>$noticia3[Capacity]</option>";
}
echo "</select>";
echo "<input type=submit value=Submit>";
echo "</form>";
?>
</body>
</html>



so ive put the code in below
i can understand what the problem is i can see it from the prints statments at the beginning, the second select box doesnt seem to be posting the results in the subcat place its posting it into the cat which isnt where i want it to be

thanks
Was This Post Helpful? 0
  • +
  • -

#7 didgy58  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 246
  • Joined: 23-October 07

Re: drop down menus

Posted 23 November 2007 - 09:17 AM

ok i think the problem is in the reload javascript function

function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
var val2=form.subcat.options[form.subcat.options.selectedIndex].value;
var val3=form.subcat1.options[form.subcat1.options.selectedIndex].value;
self.location='dd.php?cat=' + val;
}


is you can see it only puts the cat and the variable in

could i change this to the following
self.location='dd.php?cat=' + val,' subcat=' + val2,' subcat1=' + val3;


Was This Post Helpful? 0
  • +
  • -

#8 nav33n  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 71
  • Joined: 02-October 07

Re: drop down menus

Posted 23 November 2007 - 11:29 AM

Quote

could i change this to the following
CODE

self.location='dd.php?cat=' + val,' subcat=' + val2,' subcat1=' + val3;


self.location='dd.php?cat=' + val + '&subcat=' + val2 + '&subcat1=' + val3;

That's the way.

Edit: But for the first run, when this javascript function is called, the url would be, dd.php?cat=1&subcat=&subcat1= . This is because, the value of val2 and val3 will be empty !

This post has been edited by nav33n: 23 November 2007 - 11:33 AM

Was This Post Helpful? 0
  • +
  • -

#9 didgy58  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 246
  • Joined: 23-October 07

Re: drop down menus

Posted 24 November 2007 - 04:43 AM

would it still achieve the result needed even thought the other 2 are empty..thanks agin

nav33n your help has been much appreciated
Was This Post Helpful? 0
  • +
  • -

#10 didgy58  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 246
  • Joined: 23-October 07

Re: drop down menus

Posted 24 November 2007 - 04:59 PM

thanks va33n got it working nicely now was just wondering though if after the user has made there 3 selections if it was possible for me to then print out the slection they made for instance "apple" "ipod nano" "8gb" on the screen the only trouble is the reload javascript function doesnt keep the variables stored it loses them everytime there is a reload, how would i go about saving each selection made even after a reload has been done??
Was This Post Helpful? 0
  • +
  • -

#11 nav33n  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 71
  • Joined: 02-October 07

Re: drop down menus

Posted 25 November 2007 - 11:17 PM

Well, you can have a hidden field in the form. When the user selects an option from the dropdown list, you can call the javascript function, add the selected value to this hidden variable and then submit the form. So, the selected value will be accessible even after the page is submitted. You can use this hidden value to fetch the values to the next dropdown. For example,

<?php //test.php
$conn=mysql_connect("host","username","password");
mysql_select_db("database");
$selected_value=$_REQUEST['select1'];
?>
<html>
<head>
<script language="javascript">
function reload_page(str){
	document.form.select1.value=str;
	document.form.action="test.php";
	document.form.submit();
	return true;
}
</script>
</head>
<body>
<form name="form" method="post">
<input type="hidden" name="select1">
<SELECT NAME="list1" onchange="java script: return reload_page(this.value);";>
<?php
$result=mysql_query("select columnname from tablename");
$option="";
while($row=mysql_fetch_array($result)){
	if($selected_value==$row['columnname']){
		$selected="selected";
	} else { 
		$selected="";
	}
	$option.="<option value='".$row['columnname']."'".$selected.">".$row['columnname']."</option>";
}
echo $option;
?>
</SELECT>
<SELECT NAME="list2">
<?php
	$option1="";
	$result=mysql_query("select columnname2 from tablename where condition='$selected_value'");
	while($row=mysql_fetch_array($result)){
	$option1.="<option value=".$row['columnname2'].">".$row['columnname2']."</option>";
}
echo $option1;

?>
</SELECT>
<INPUT TYPE="button" name="button" value="submit" onclick='java script: document.form.action="test.php"; document.form.submit()";>
</form>
</body>
</html>


This will submit the page whenever the user changes the value in the 1st dropdown list. That value is stored in select1 which can be used to fetch the values for 2nd dropdown list.
Was This Post Helpful? 0
  • +
  • -

#12 didgy58  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 246
  • Joined: 23-October 07

Re: drop down menus

Posted 26 November 2007 - 04:23 AM

so lets say i have three boxes and i would like to create a sql statment out of all three selections i could have three hidden fields and fill them with the data from each one and then build a statment out of them.

could i also use the hidden field value so when the page has reloaded the selected value of the previous select is still showing the value that was selected say for instance apple was selected u refresh it then loses this and goes back to please select with my reload, could i then use the hidden value to make that selected value = apple again so the user knows what they selected in the select incase they had forgotten

thanks again nav33n
Was This Post Helpful? 0
  • +
  • -

#13 nav33n  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 71
  • Joined: 02-October 07

Re: drop down menus

Posted 26 November 2007 - 06:11 AM

yep..but, its kinda redundant.. if u know ajax( well, i dont know :( ) u can try doing the same..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1