Jump to content

Dynamic Dropdown


richo89

Recommended Posts

I have created a dynamic dropdown that consists of a user selecting a car make, for example Alfa Romeo - and the models listed are dynamically chosen dependent on the make.

 

I've edited code I found on the net but just need some help with it echoing the correct text.

 

My code is as follows for the dynamic drop down.

 

<?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';
// username and password to log onto db server
$dbusername='root';
$dbpassword='';
// name of database
$dbname='cars';

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

/*
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 DISTINCT category,cat_id FROM category order by category"); 
///////////// 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 DISTINCT subcategory FROM subcategory where cat_id=$cat order by subcategory"); 
}else{$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory order by subcategory"); } 
////////// 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>";
?>

 

I then have a dd-check.php file which echoes the following:

 

<?
$cat=$_POST['cat'];
$subcat=$_POST['subcat'];

echo "Value of \$cat = $cat <br>Value of \$subcat = $subcat ";


?>

 

However the code echoes the Category ID which for example Alfa Romeo is 1, Austin is 2, Bentley 3 and so fourth.

 

Therefore my echo shows Cat = 1 Subcat = 147

 

How can I get this so that it echoes Cat = Alfa Romeo Subcat = 147

Link to comment
https://forums.phpfreaks.com/topic/152282-dynamic-dropdown/
Share on other sites

The code already queries the Cat value and the sub categories by the subcat value surely?

 

Because its echoing the correct subcat however my category is echoing 1 rather than alfa romeo.

 

My table for category looks like this:

1 - Alfa Romeo

2 - Austin

3 - Bentley

 

And therefore cat value of $cat is 1 - however I need this to echo value of $cat is Alfa Romeo

Link to comment
https://forums.phpfreaks.com/topic/152282-dynamic-dropdown/#findComment-799778
Share on other sites

Only two weeks ago I worked on a system similar about car bulbs.

 

Car manufacturers about 58 rows. Car models 1,224 rows, 1,672 rows of data for car years and with the bulb data populated completely there's over 25,000 rows of data!

 

The workaround I used was storing everything in a database - I'm presuming you're doing the same what with the amount of data although I'm not sure where you're going with your car data.

 

Instead of using Javascript to populate the next drop-down box on selection of the previous box I used Javascript to reload the page with an onchange of the first box.

Link to comment
https://forums.phpfreaks.com/topic/152282-dynamic-dropdown/#findComment-800118
Share on other sites

$cat=$_POST['cat'];
$subcat=$_POST['subcat'];

 

this code gets the values you are passing with your form...

and those values are the IDs of the cars

 

if you want to print the names you will either have to pass the car names as value in your form either get the car name associated with the ID with a query like this

SELECT category FROM category WHERE cat_id = $catId

Link to comment
https://forums.phpfreaks.com/topic/152282-dynamic-dropdown/#findComment-800123
Share on other sites

$cat=$_POST['cat'];
$subcat=$_POST['subcat'];

 

this code gets the values you are passing with your form...

and those values are the IDs of the cars

 

if you want to print the names you will either have to pass the car names as value in your form either get the car name associated with the ID with a query like this

SELECT category FROM category WHERE cat_id = $catId

 

Many thanks I will look into this now!

 

And yes I do have a database full of data with a large amount of records Yesideez i'll also look into the info you provided.

 

Thanks for the help i'll let you know how it goes.

Link to comment
https://forums.phpfreaks.com/topic/152282-dynamic-dropdown/#findComment-800127
Share on other sites

Still having troublr with this  :(

 

Query:

$category=mysql_query("SELECT category FROM category WHERE cat_id = $cat");

 

I then try in my DD-Check.php to echo $category

$category=$_POST['category'];
echo "Make: $category";

 

Cant beleive cant get this working!

 

this

$category=mysql_query("SELECT category FROM category WHERE cat_id = $cat");

should be in your DD-Check.php file

Link to comment
https://forums.phpfreaks.com/topic/152282-dynamic-dropdown/#findComment-800149
Share on other sites

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.