douxian5963 2014-03-10 10:11
浏览 60

如何根据另一个选择框项目,反之亦然

how to select the select box item based on one another and vice versa.here I use ajax for redirect to query page.

while($fet = mysql_fetch_assoc($sql1)) 
{
    echo "<option value=".$fet['username'].">".$fet['username']."</option>";
}

echo '</select></td>';
echo "<td><div id='myDiv'><select id=id name=id onchange=loadXMLDoc()>";

$sql = "select * from sample";
$sql1 = mysql_query($sql);
while($fet = mysql_fetch_assoc($sql1))
{
    echo "<option value=".$fet['id'].">".$fet['id']."</option>";
}
echo '</select></div></td>';
  • 写回答

1条回答 默认 最新

  • doucheng1063 2014-03-10 10:27
    关注

    You Need To Use Ajax For That, For Example You Have A Select like This

    <select id=id1 name=id1 onchange=loadXMLDoc()> here is the second one you want to change <select id=id2 name=id2>

    On Javascript You Call a php file via ajax Like This

    function loadXMLDoc(){
             $.ajax({ // i think you used jquery on your project
                type: "post",
                url: "getdata.php",
                data: $('input[\'name=id1\']'),
                dataType: "json",
                success:(function(result){
                        html = '';
                       if(result.data.length > 0){
                           $.each(result.data, function( index, value ) {
                              //  alert( index + ": " + value );
                                html += '<option value="'+value.id+'">'+value.title+'</option>';
                            });
                            $('#id2').html(html); 
                       }else{
                         // nothing
                       }
                 }));
    
            }); 
    
       }
    

    then on php file you just echo the json_encode result you want to get

    $sql = mysql_query('SELECT id,title FROM your_table Where your_field='.$_POST['id1']);
    $query= mysql_query($sql);                
    while( $row = mysql_fetch_array($query)){
       $json['data'][] = array('id' =>$row->id,'title'=>$row->title )
                }
     echo json_encode($json);
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向