我想从第一个选择框的值填充第二个选择框名称'ctype‘。但是问题是,仍然没有显示我附加的值…我不知道已经2周了,我尝试solved..but仍然没有显示在第二个选择框中。
cash.php
在第一个选择框中
<?php $qryselect = mysql_query("select * from cc_merchant"); ?>      
<select id="merchant" name="merchant"  style="font:25px Arial, Helvetica, sans-serif;   font-weight:bold; color:#000000;">
     <option value=''>--Choose One--</option>
            <?php while($getrow=mysql_fetch_array($qryselect)) {?>
     <option value="<?php echo $getrow['merchant_id']; ?>" ><?php echo $getrow['bank_merchant'];  ?></option>
            <?php }  ?>
</select>第二个选择框
<p id="ctype">
    <select id="ctype_id" name="ctype_id" style="font:25px Arial, Helvetica, sans-serif; font-weight:bold; color:#000000;">    
        <option value="">--Choose One--</option>
     </select>
</p>jquery
<script type="text/javascript">
    $("#merchant").selectbox({
    onChange:function(val,inst){
            $.ajax({
                    type:"GET",
                    data:{merchant:val},
                    url:"getctype.php",
                    success:function(data){
                          $("#ctype").html(data);
                          $("#ctype_id").selectbox();
                    }
            });
         },
    });
</script>脚本getctype.php
<?php
   session_start();
   require_once("dbcon.php");
   $target = $_GET['merchant'];
   $sql = mysql_query("select card from card_support where merchant_id = '$target'");
   $getmerchant = mysql_fetch_assoc($sql);
   $card = $getmerchant['card'];
   echo $card."\n";
?>这是I follow...the值从第一个选择框填充到第二个选择框的示例。http://www.bulgaria-web-developers.com/projects/javascript/selectbox/index.php
发布于 2012-12-14 12:13:03
首先,you getctype.php正在请求数据库,并且只返回一张卡。也许你应该对结果进行迭代,以便返回多个值。此外,建议返回JSON数据(可以通过Javascript轻松管理)。你可以用一段代码来完成这项工作,比如
<?php
// start the session and load your libs
session_start();
require_once("dbcon.php");
// Ask DB for result and store them in an array
$sql = mysql_query("select card from card_support where merchant_id = '$target'");
$result = array();
while($getmerchant = mysql_fetch_assoc($sql))
    $result[] = $getmerchant;
// Send JSON header (no cache)
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
// Finally return the array in JSON format
echo json_encode($result);PHP脚本返回的数据将是JSON响应,如下所示:
["card1","card2","card3",....]其次,当我们查看AJAX调用时,成功函数将HTMLElement替换为id ctype
$("#ctype").html(data);id为ctype的HTMLElement是一个<p>,因此<p>的内容完全替换为您的原始内容(卡值),而不是a。然后,您的第二行代码:
$("#ctype_id").selectbox();将不会有任何效果,因为不再存在(替换为从AJAX调用返回的内容)。
要使其工作,您可以在JS中使用这段代码:
// Assume the second select is already a **selectbox**    
// Make the first select a selectbox
$("#merchant").selectbox({
    onChange:function(val,inst){
        // Run the ajax call to refresh the content of the second selectbox
        $.ajax({
            type: "GET",
            data: { merchant: val },
            url:"getctype.php",
            dataType: 'json',
            success: function(data) {
                // Remove previous content of your second selectbox
                $("#ctype_id").empty();
                // Append default option
                $("#ctype_id").append($('<option value="">-- Chose one --</option>'));
                // Loop on your data (which is an array)
                for(var i = 0 ; i < data.length ; i++)
                {
                    $("#ctype_id").append($(document.createElement("option"))
                                          .val(data[i])
                                          .html(data[i]));
                }
            }
        });
    }
});发布于 2012-12-14 12:00:49
如果您使用jquery,我应该执行以下操作:
 $("#merchant").on('change', function() {
 $('#ctype_id').empty();
 $.get('getctype.php', function(data) {
 $(data).appendTo('#ctype_id');
 });
 });你的php代码应该是这样的:
echo '<option value="">--Choose One--</option>'
$sql = mysql_query("select card from card_support where merchant_id = '$target'");
$getmerchant = mysql_fetch_assoc($sql);
$card = $getmerchant['card'];
echo '<option value="'.$card.'">'.$card.'</option>';https://stackoverflow.com/questions/13872213
复制相似问题