duan246558 2014-02-16 12:18
浏览 30

使用PHP和Javascript与JQuery选择另一个选择选项后更新html选择选项

Consider the following structure:

A table named "Regions" with the columns "id","name".

A table named "Cities" with the columns "id","region_id","name".

What is the best way to repopulate an html select options using PHP and Javascript?

For example lets say I have the following php code:

<select name = "region">
<option value = "">Select Region</option>
<?
 foreach ($regions as $region)
 {
  echo "<option value = '$region[id]'>region[name]</option>";
 }
?>
</select>

<select name = "city">
 <option value = "">Select City</option>
</select>

Once a region has been selected, all the cities with the proper foreign key "region_id" that corresponds to the selected region, will be populated into the cities select.

Assume that I use this kind of structure a lot, so I do want to have a minimal, easy to reuse code.

  • 写回答

1条回答 默认 最新

  • dongzaijiao4863 2014-02-16 13:21
    关注

    HTML

    <select class="selectbox" name = "region" data-table="regions" data-parent="parent_id" data-name="name" data-target="targetselect">
        <option value = "">Select Region</option>
        <? foreach ($regions as $region): ?>
        <option value = '<?php echo $region[id];?>'><?php echo region[name]; ?></option>
        <?php endforeach;?>
    </select>
    
    <select name = "city" class="targetselect">
        <option value = "">Select City</option>
    </select>
    

    * JS *

    jQuery(document).ready(function($){
        $('.selectbox').on('change', function(e) {
            var target = $(this).data('target');
            var data = $(this).data();
            data.key = $(this).val();            
    
            $.ajax({
                url: 'ajax.php',
                data: data,
                type: 'POST'
            }).success(function(response){
                //check if response
                if(response.items) {
                     $.each(response.items, function(){
                         target.append($('<option></option>')
                             .attr('value', this.id)
                             .text(this.name));
                     });
                }
            });
        })
    });
    

    * PHP *

    /***
     * Check if You have all required data
     **/
    
     if(isset($_POST['table']) &&  .... ) {
         /**
          * Do all security checks for example if table is allowed to query etc
          **/
         $query = 'SELECT required_cols ....';
         $result = /** Query results **/;
         echo json_encode($result, true);
     }
    

    it's lots of not checked code, i was writing it without testing, but should give you idea where to start

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么