我目前正在使用动态下拉菜单(可靠的选择框)。我直接从 MySQL DB 中提取值(如果您在这里好奇我是如何拥有DB SETUP的)。我能够获取每个表的值并相应地显示它们。我遇到的问题是回显每个选择框的 SELECTED 值。我有一个创建的 JS 函数,它将请求 postfile.php,然后将回显每个框的 SELECTED 值。我没有得到任何回应。我已经检查过萤火虫,但没有发布任何内容。
我怎样才能使这项工作?还是我接近这个错误?或者,还有更好的方法?例子
工作 HTML/PHP
<?php
include ('includes/dbConnect.php');
try {
$pdo = get_database_connection();
$sql = "SELECT *
FROM `categories`
WHERE `master_id` = 0";
$statement = $pdo->query($sql);
$list = $statement->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo 'There was a problem';
}
?>
<select name="main" id="main" size="7" class="update">
<option value="">Select one</option>
<?php if (!empty($list)) { ?>
<?php foreach($list as $row) { ?>
<option value="<?php echo $row['id']; ?>">
<?php echo $row['name']; ?>
</option>
<?php } ?>
<?php } ?>
</select>
<select name="subc1" id="subc1" size="7" class="update" disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
<select name="subc2" id="subc2" size="7" class="update" disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
<select name="subc3" id="subc3" size="7" class="update" disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
JS
<script type="text/javascript">
$(document).ready(function () {
$('#main).change(function() {
if ($(this).val()!='
') {
$("#subc1").load("postfile.php",{main_id: $(this).val()});
//$("#subc1").removeAttr('
disabled hidden ');
}
});
//code on change of sel_source
$('#subc1 ').change(function() {
if ($(this).val()!='
') {
$("#subc2").load("postfile.php",{subc1_id: $(this).val()});
//$("#colour").removeAttr('
disabled ');
}
});
$('#subc2 ').change(function() {
if ($(this).val()!='
') {
$("#subc3").load("postfile.php",{subc2_id: $(this).val()});
//$("#colour").removeAttr('
disabled ');
}
});
});
</script>
PHP- postfile.php
if(isset($_REQUEST['main_id']) && !empty($_REQUEST['main_id'])) {
try {
include ('../includes/dbConnect.php');
$pdo = get_database_connection();
$sql = ("select * from `categories` where id='".$_REQUEST['main_id']."' ");
$result = $con->prepare($sql);
$result->execute();
$number_of_rows = $result->fetchColumn();
}catch(PDOException $e) {
echo 'There was a problem';
}
if($number_of_rows > 0) {
$output = '<option value="">Select</option>';
while($row = mysql_fetch_assoc($result)) {
$output .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
} else {
$output = '<option value="">Select</option>';
}
echo $output;
}
if(isset($_REQUEST['subc1_id']) && !empty($_REQUEST['subc1_id'])) {
$result = mysql_query("select * from table where id='".$_REQUEST['subc1_id']."' ");
if($number_of_rows > 0) {
$output = '<option value="">Select</option>';
while($row = mysql_fetch_assoc($result)) {
$output .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
} else {
$output = '<option value="">Select</option>';
}
echo $output;
}
if(isset($_REQUEST['subc2_id']) && !empty($_REQUEST['subc2_id'])) {
$result = mysql_query("select * from table where id='".$_REQUEST['subc2_id']."' ");
if($number_of_rows > 0) {
$output = '<option value="">Select</option>';
while($row = mysql_fetch_assoc($result)) {
$output .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
} else {
$output = '<option value="">Select</option>';
}
echo $output;
}