PHPカスケードメニュー、AJAX版以外
4519 ワード
<?php
//
$category = array(
1 => array(
'id' => 1,
'name' => ' '
),
2 => array(
'id' => 2,
'name' => ' '
),
);
//
$children = array(
1 => array(
15 => array(
'id' => 15,
'name' => ' '
),
16 => array(
'id' => 16,
'name' => ' '
),
),
2 => array(
20 => array(
'id' => 20,
'name' => ' '
),
21 => array(
'id' => 21,
'name' => ' '
)
),
);
// ID
$father_id = 2;
// ID
$son_id = 21;
?>
<html>
<head></head>
<body>
<div class="search">
<form action="site.php" method="get">
<input type="hidden" name="act" value="module" />
<input type="hidden" name="do" value="goods" />
<input type="hidden" name="op" value="display" />
<input type="hidden" name="name" value="shopping" />
<table class="table table-bordered tb">
<tbody>
<tr>
<th> </th>
<td>
<select class="span3" style="margin-right:15px;" name="cate_1" onchange="fetchChildCategory(this.options[this.selectedIndex].value);">
<option value="0"> </option>
<?php foreach ($category as $row) { ?>
<option value="<?php echo $row['id']; ?>" <?php if ($row['id'] == $father_id) { ?> selected="selected"<?php } ?>><?php echo $row['name']; ?></option>
<?php } ?>
</select>
<select class="span3" id="cate_2" name="cate_2">
<option value="0"> </option>
<?php if (isset($son_id) && isset($children[$father_id])) { ?>
<?php foreach ($children[$father_id] as $row) { ?>
<option value="<?php echo $row['id']; ?>" <?php if ($row['id'] == $son_id) { ?> selected="selected"<?php } ?>><?php echo $row['name']; ?></option>
<?php } ?>
<?php } ?>
</select>
</td>
</tr>
<tr>
<tr class="search-submit">
<td colspan="2"><button class="btn pull-right span2"><i class="icon-search icon-large"></i> </button></td>
</tr>
</tbody>
</table>
</form>
</div>
<script type="text/javascript">
var category = <?php echo json_encode($children); ?>;
function fetchChildCategory(cid) {
var html = '<option value="0"> </option>';
if (!category || !category[cid]) {
$('#cate_2').html(html);
return false;
}
for (i in category[cid]) {
html += '<option value="' + category[cid][i][0] + '">' + category[cid][i][1] + '</option>';
}
$('#cate_2').html(html);
}
</script>
</body>
</html>