Dynamic Dependent Select Boxes
hi all,
i trying create dynamic dependent select box, 2 teir , having problems. using tutorial here: http://www.invlessons.com/change-contents-dynamically-of-a-select-box-depends-on-another-s elect-box-using-jquery-ajax-php-and-mysql/
my problem style of tutorial doesn't fit site well, , don't want have many redundant files located everywhere. trying change accordingly. mine not displaying errors lost. there 2 main files database connections files well...
db.php
$write = array('host' => 'localhost',
'username' => 'username',
'password' => 'password',
'dbname' => 'dbname');
$read = array('host' => 'localhost',
'username' => 'username',
'password' => 'password',
'dbname' => 'dbname');
index.php
<script type="text/javascript">
$(document).ready(function()
{
$("#parent_category").change(function()
{
var category_id = $(this).val();
if(category_id != '')
{
$.ajax
({
type: "post",
url: "get_child.php",
data: "category_id="+ category_id,
success: function(option)
{
$("#child_category").html(option);
}
});
}
else
{
$("#child_category").html("<option value=''>-- no category selected --</option>");
}
return false;
});
});
</script>
<title>sections demo</title>
<body>
<span>select category:</span>
<select id="parent_category" name="parent_category" style="">
<?php foreach ($categories2 $row) {
echo "<option value='{$row['category_id']}'";
if ($errors && $_post["category_id"] == $row['category_id']) {
echo 'selected="selected"';
}
echo ">{$row['category_name']}</option>";
}
?>
?>
</select>
<span>available brands:</span>
<select id="child_category" name="child_category">
<option value="">-- no category selected --</option>
</select>
and get_child.php
<?php
include('db.php');
?>
<?php
if(isset($_post['category_id']) && $_post['category_id'] != '')
{
$category_id = $_post['category_id'];
$sub_categories = getallsubcategories($dbread, $categories2);
}
function getallsubcategories($read, $category_id) {
$sql = $read->quoteinto("select *
sub_categories
category_id = '".$category_id."'");
$result = $read->fetchall($sql);
if ($result)
{
while($row = $result)
{
echo "<option value='".$row['sub_category_id']."'>".ucfirst($row['sub_category_name'])."</option>";
}
}
}
?>
i appreciate , if spot out mistakes on part. thank you.
More discussions in Develop server-side applications in Dreamweaver
adobe
Comments
Post a Comment