Problem with chained select from MySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Eiolon
Forum Newbie
Posts: 17
Joined: Tue Feb 14, 2006 1:42 pm

Problem with chained select from MySQL

Post by Eiolon »

I am not sure if it is my PHP or Javascript that is causing this to not work. The first select gets populated successfully but the second doesn't get populated when something is selected from the first:

Code: Select all

<?php

require_once('../includes/support.php');
require_once('../includes/functions.php');

@$cat=$_GET['cat'];

if(strlen($cat) > 0 and !is_numeric($cat)) {
	echo "Error";
	exit;
}

$query_categories = mysql_query("SELECT DISTINCT id, category FROM categories ORDER BY category ASC"); 

if(isset($cat) and strlen($cat) > 0){
	$query_equipment = mysql_query("SELECT DISTINCT name, description FROM equipment WHERE category_id=$cat"); 
} else {
	$query_equipment = mysql_query("SELECT DISTINCT name, description FROM equipment WHERE category_id=0");
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Support</title>
<link href="../includes/style.css" rel="stylesheet" type="text/css">
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='index2.php?cat=' + val ;
}
</script>
</head>

<body>

<?php include_once('../includes/menu.php'); ?>

<div id="headers">
<img src="../images/support.png" class="icon" alt="Support" /><h1>Support</h1>
</div>

<div id="content">

<form method="post" name="add" action="">

<label>Category:</label>

<select name="cat" id="cat" onchange=\"reload(this.form)\">
<option value=''>SELECT</option>
<?php while($category = mysql_fetch_array($query_categories)) {
	if($category['id']==@$cat) {
		echo "<option selected value='$category[id]'>$category[category]</option>"."<br>";
	} else { 
		echo "<option value='$category[id]'>$category[category]</option>";
	}
}
?>
</select>

<label>Equipment:</label>

<select name="equipment_id" id="equipment_id">
<option value=''>SELECT</option>
<?php while($equipment = mysql_fetch_array($query_equipment)) { print '<option value="' . $equipment['name'] . '</option>';}?>
</select>


<input type="submit" value="Submit">

</form>



</div>

</body>
</html>
Thanks for your assistance!
Eiolon
Forum Newbie
Posts: 17
Joined: Tue Feb 14, 2006 1:42 pm

Re: Problem with chained select from MySQL

Post by Eiolon »

Nevermind, I got it working
Post Reply