Page 1 of 1

php, mysql, drop down boxes

Posted: Fri Dec 31, 2010 4:09 pm
by jsk1gcc
hi, I'm not 'really' new to sql or php but it has been some time since i have created anything with them.

This is the first time I have ever attempted double drop boxes. What I am trying to create is a simple booking system. When an option is chosen from the first drop down the page reloads with options changing in the second drop down box for the user to choose.

I have found an example during my research, when trying to modify it to suit my needs I seem to have got confused somewhere. I spent quite some time trying to find out my mistake but alas, nothing yet.

I would appreciate some help =)


tables used in boxes:
[text]
CREATE TABLE `ride`
(
`RrideID` tinyint(1) default NULL,
`name` varchar(20) default NULL,
`time` tinyint(1) default NULL,
`price` varchar(4) default NULL
)

INSERT INTO `ride`
VALUES (1, 'Swinging Ship', 7, '2.50');
INSERT INTO `ride` VALUES (2, 'Roller Coaster', 5, '3.75');
INSERT INTO `ride` VALUES (3, 'Ice Blast', 4, '3.00');

CREATE TABLE `seats`
(
`seatID` int(8) NOT NULL default '0',
`SrideID` tinyint(1) default NULL,
`seatNumber` int(2) default NULL,
PRIMARY KEY (`seatID`)
)

INSERT INTO `seats` VALUES (1, 1, 1);
INSERT INTO `seats` VALUES (2, 1, 2);
INSERT INTO `seats` VALUES (3, 1, 3);
INSERT INTO `seats` VALUES (4, 1, 4);
INSERT INTO `seats` VALUES (5, 1, 5);

INSERT INTO `seats` VALUES (49, 2, 1);
INSERT INTO `seats` VALUES (50, 2, 2);
INSERT INTO `seats` VALUES (51, 2, 3);
INSERT INTO `seats` VALUES (52, 2, 4);
INSERT INTO `seats` VALUES (53, 2, 5);

INSERT INTO `seats` VALUES (69, 3, 1);
INSERT INTO `seats` VALUES (70, 3, 2);
INSERT INTO `seats` VALUES (71, 3, 3);
INSERT INTO `seats` VALUES (72, 3, 4);
INSERT INTO `seats` VALUES (73, 3, 5);
[/text]




The tutorial is from here: http://www.plus2net.com/php_tutorial/ph ... n_list.php

this is what I have done.

Code: Select all

<?php 
//CONNECT TO DATABASE
$username = '*******'; 
$password = '************'; 
$host = '********';
$dbname = '********'; 

$connection = mysql_connect($host, $username, $password);
	if (!$connection) {
		die ('connection failed' . mysql_error());
		}
		echo 'Connected sucessfully';
//////edit out echo once done/////

$select = mysql_select_db($dbname);
	if (!$select) {
		die('select not working' . mysql_error());
		}
		echo 'selected sucessfully';
//////edit out echo once done////		
?>

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

<html>

<head>
<title>Ultra Rides</title>
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.ride.options[form.ride.options.selectedIndex].value;
self.location='index.php?ride=' + val ;
}
</script>
</head>

<body>
<?

@$ride=$_GET['RrideID'];
if(strlen($ride) > 0 and !is_numeric($ride)){ 
echo "Data Error";
exit;
}

// data from Mysql for first list box
$Rquery=mysql_query("SELECT DISTINCT RrideID,name FROM ride order by RrideID"); 
//End of query for first list box

//for second drop down list we will check if category(ride) is selected else we will display all the subcategory(seats)
if(isset($ride) and strlen($ride) > 0){
$MATCHq=mysql_query("SELECT DISTINCT seatNumber FROM seats where SrideID=$ride ORDER by seatNumber"); 
}else{$MATCHq=mysql_query("SELECT DISTINCT seatNummber FROM seats ORDER by seatNumber"); } 
//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='ride' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
while($x = mysql_fetch_array($Rquery)) { 
if($x['RrideID']==@$ride){echo "<option selected value='$x[RrideID]'>$x[name]</option>"."<BR>";}
else{echo  "<option value='$x[RrideID]'>$x[name]</option>";}
}
echo "</select>";
//This will end the first drop down list 

//Starting of second drop downlist
echo "<select name='seats'><option value=''>Select one</option>";
while($y = mysql_fetch_array($MATCHq)) { 
echo  "<option value='$y[seats]'>$y[seats]</option>";
}
echo "</select>";
//This will end the second drop down list 

//// do rest of form when done/////
echo "<input type=submit value=Submit>";
echo "</form>";
?>

</body>
</html>

any help would be fantastic. Thanks :)