Page 1 of 1

Insert value of cat_id instead value of category in php drop

Posted: Thu May 03, 2007 4:57 am
by Mochachino
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi all

I really need some help here. I created 3 drop down dynamic menu. However, In my insert page, I only can get the value of the first and second drop down as cat_id / subcat_id instead of category/ subcategory value. The third drop down value is fine.

The code is as below:
[syntax="html"]
<SCRIPT language=JavaScript>
function reload(form)
{var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='preview_booking.php?cat=' + val ;
}
function reload3(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
var val2=form.subcat.options[form.subcat.options.selectedIndex].value;
self.location='preview_booking.php?cat=' + val + '&cat3=' + val2 ;
}
</script>
[/syntax]

Code: Select all

<?php
$dbservertype='mysql';
$servername='localhost';
// username and password to log onto db server
$dbusername='';
$dbpassword='';
// name of database
$dbname='';

////////Connecting to database///////
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 ////////
?>
<?


///////// 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///// 
$cat=$HTTP_GET_VARS['cat']; // This line is added to take care if your global variable is off
if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT DISTINCT subcategory,subcat_id FROM subcategory where cat_id=$cat order by subcategory"); 
}else{$quer=mysql_query("SELECT DISTINCT subcategory,subcat_id FROM subcategory order by subcategory"); } 
////////// end of query for second subcategory drop down list box ///////////////////////////


/////// for Third drop down list we will check if sub category is selected else we will display all the subcategory3///// 
$cat3=$HTTP_GET_VARS['cat3']; // This line is added to take care if your global variable is off
if(isset($cat3) and strlen($cat3) > 0){
$quer3=mysql_query("SELECT DISTINCT subcat2 FROM subcategory2 where subcat_id=$cat3 order by subcat2"); 
}else{$quer3=mysql_query("SELECT DISTINCT subcat2 FROM subcategory2 order by subcat2"); } 
////////// end of query for third subcategory drop down list box ///////////////////////////


//echo "<form method=post name=f1 action=''>";
//////////        Starting of first drop downlist /////////
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select Venue</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' onchange=\"reload3(this.form)\"><option value=''>Select Day</option>";
while($noticia = mysql_fetch_array($quer)) { 
if($noticia['subcat_id']==@$cat3){echo "<option selected value='$noticia[subcat_id]'>$noticia[subcategory]</option>"."<BR>";}
else{echo  "<option value='$noticia[subcat_id]'>$noticia[subcategory]</option>";}
}
echo "</select>";
//////////////////  This will end the second drop down list ///////////


//////////        Starting of third drop downlist /////////
echo "<select name='subcat3' ><option value=''>Select Time</option>";
while($noticia = mysql_fetch_array($quer3)) { 
echo  "<option value='$noticia[subcat2]'>$noticia[subcat2]</option>";
}
echo "</select>";
//////////////////  This will end the third drop down list ///////////


//echo "</form>";
?>
Code for insert page:

Code: Select all

<?PHP

// Connect database


$region = $_REQUEST['region'];
$person = $_REQUEST['person'];
$venue = $_REQUEST['cat'];
$day = $_REQUEST['subcat'];
$supersubcat = $_REQUEST['subcat3'];
$gender = $_REQUEST['gender'];
$race = $_REQUEST['race'];
$countryCode = $_REQUEST ['countryCode'];
$areaCode = $_REQUEST ['areaCode'];
$telNo = $_REQUEST ['telNo'];
$occupation = $_REQUEST ['occupation'];
$childage1 = $_REQUEST ['childage1'];
$childage2 = $_REQUEST ['childage2'];
$childage3 = $_REQUEST ['childage3'];
$childage4 = $_REQUEST ['childage4'];
print ("$venue <br>");
print ("$day <br>");
print ("$supersubcat");



$con = mysql_connect("localhost","","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("", $con);


if(isset($_REQUEST['Submit'])) {	
   	 
    $sql="INSERT INTO tbl_book (region, person,venue,day,timeslot, gender, race, countryCode, areacode, telNo, occupation, childage1, childage2, childage3, childage4 )
VALUES('$region','$person','$venue','$day','$supersubcat','$gender','$race','$countryCode', '$areaCode','$telNo', '$occupation','$childage1', '$childage2', '$childage3', '$childage4')";		

   
  $result = mysql_query($sql,$con);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
else
{
echo '<h3>Your Preview Booking has been submitted successfully!</h3><br>';

echo '<a href="index.html">Click here</a> to go back to HOME page';

}
mysql_close();
}

//mysql_query("INSERT INTO tbl_book (venue) VALUES (".$_POST['select'].")");
?>
Database structure
<code>

Code: Select all

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

CREATE TABLE `subcategory` (
  `subcat_id` int(3) NOT NULL auto_increment,
  `cat_id` int(2) NOT NULL default '0',
  `subcategory` varchar(25) NOT NULL default '',
  UNIQUE KEY `subcat_id` (`subcat_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;

CREATE TABLE `subcategory2` (
  `subcat_id` int(3) NOT NULL default '0',
  `subcat2` varchar(15) NOT NULL default ''
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
</code>

Please help me to identify the mistakes.
Thank you in advance.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]