Jump to content

Multiple Dropdown can't pass right variable


Recommended Posts

I am trying to accomplish a custom State / City drop down using a database and I am running into a small problem of pulling the wrong variable (field from the database). My end result is this

 

State is 4

City is Jonesboro

 

As you can see there are no states called "4", that is the cat_id assigned to the state which would be Arkansas.

 

I have gotten to the point where I need an extra eye to point out what I am doing wrong because I am doing more harm than good right now deleting/adding code just to see if it works....

 

 

<?php

@$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'>";
//////////        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>";
?>

 

 

the second page "dd-check.php" is simple

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

echo "State is $cat <br>City is $subcat ";

?>

 

The $cat has a result of 4 or I should say a NUMERIC value of the state and need to be able to find a way to pull the result of the state instead.....

 

 

Additional Info (database architecture if this helps)

CREATE TABLE `category` (
  `cat_id` int(2) NOT NULL auto_increment,
  `category` varchar(90) NOT NULL,
  PRIMARY KEY  (`cat_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=52 ;

INSERT INTO `category` VALUES(1, 'AL');
INSERT INTO `category` VALUES(2, 'AK');
INSERT INTO `category` VALUES(3, 'AZ');
INSERT INTO `category` VALUES(4, 'AR');



CREATE TABLE `subcategory` (
  `cat_id` int(2) NOT NULL default '0',
  `subcategory` varchar(90) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO `subcategory` VALUES(4, 'Ft. Smith-Fayetteville-Springdale-Rogers');
INSERT INTO `subcategory` VALUES(4, 'Jonesboro');
INSERT INTO `subcategory` VALUES(4, 'Little Rock-Pine Bluff');
INSERT INTO `subcategory` VALUES(4, 'Springfield');

 

 

echo "<form method=get 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($quer2)) { 
if($noticia2['category']==@$cat){echo "<option selected value='$noticia2[category]'>$noticia2[category]</option>"."<BR>";}
else{echo  "<option value='$noticia2[category]'>$noticia2[category]</option>";}
}
echo "</select>";

 

If i choose to switch the first drop down, i can get it to show the state abbreviations but then the city field never populates so it's pretty much in reverse and getting worse, still trying and was just giving anyone more information in case they were curious if i tried something or not.

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.