Jump to content

Problem with chained select from MySQL


Eiolon

Recommended Posts

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:

 

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

Link to comment
https://forums.phpfreaks.com/topic/204978-problem-with-chained-select-from-mysql/
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.