gpt4 book ai didi

PHP/JS : Dynamic Dropboxes and echoing SELECTED value

转载 作者:行者123 更新时间:2023-11-30 06:40:35 25 4
gpt4 key购买 nike

我目前正在使用动态下拉菜单(可靠的选择框)。我直接从 MySQL DB 中提取值(如果你好奇我是如何得到 DB SETUP 的)。我能够获取每个表的值并相应地显示它们。我遇到的问题是回显每个选择框的 SELECTED 值。我创建了一个 JS 函数,它将请求 postfile.php,然后回显每个框的 SELECTED 值。我没有得到任何回应。我已经检查过 Firebug ,但没有发布任何内容。

我怎样才能使这项工作?还是我接近这个错误?或者,还有更好的方法? EXAMPLE

工作 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;
}

最佳答案

也许您想使用 jQuery UI 自动完成功能。它更易于使用且代码更少。它也有一个远程数据源。试试这个,也许这可以解决您的问题。 http://jqueryui.com/demos/autocomplete/

关于PHP/JS : Dynamic Dropboxes and echoing SELECTED value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11596909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com