dpwuvfpu52502 2016-09-22 09:03
浏览 39
已采纳

使用php-Ajax动态填充数据库中的每一行

I have a table that is dynamically populated from mysql database.User are expected to select a staff number, which automatically goes to the DB and fetches his staff number.I have like 10rows. it works fine for the first row but not the subsequent other others.

Please, take a look at the code and advice where I am missing it.

Thanks

<tr>
    <th nowrap="nowrap">S/N</th>
    <th nowrap="nowrap">VNO</th>
    <th nowrap="nowrap">Name</th>
    <th nowrap="nowrap">Staff No</th>
</tr>
<tr>
  <?php 
  $c=0; 
  $st =mysqli_query($connection,"SELECT * FROM tab_flt WHERE mainloc='".$_SESSION['location']."' AND status='Active'"); 
  while($r = mysqli_fetch_array($st)){  $c++?> 
  <td><?php echo $c;?></td>
  <td><input type="text" name="flt[]" value="<?php echo $r['fltno'];?>" class="form-control" readonly="readonly" /></td>
  <td><select name="opname[]" class="form-control" id="subloc">
    <option>Select...</option>
    <?php  
        $fs = getOperators($_SESSION['location']); 
        while($f = mysqli_fetch_array($fs)){?>
            <option value="<?php echo $f['name'];?>"><?php echo $f['name'];?></option>
    <?php };?>
    </select></td>
  <td id="staffno"></td>
</tr>

Ajax side:

<script type="text/javascript">
$(document).ready(function() {
    $("#subloc").change(function(){
        var sname = $("#subloc option:selected").val();
        $.ajax({
            type:"POST",
            url:"process-opno.php",
            data:{opname:sname}
        }).done(function(data3){
            $("#staffno").html(data3);  
        });
    });
});
</script>

The above fetches the first rows when subloc id is selected successfully into staffno id. But it does not do it for the remaining lines. What can i do so, that it will recognise the second line, third line etc and fetches the corresponding staff number into the staffno id .

Thanks.

  • 写回答

3条回答 默认 最新

  • dongmeiwei0226 2016-09-22 09:38
    关注

    Please try this:

    PHP Part I have added classes for select box and select box ajax result

    <tr>
    <?php 
        $c=0; 
        $st =mysqli_query($connection,"SELECT * FROM tab_flt WHERE mainloc='".$_SESSION['location']."' AND status='Active'"); 
        while($r = mysqli_fetch_array($st)){  
        $c++;
    ?> 
        <td><?php echo $c;?></td>
        <td><input type="text" name="flt[]" value="<?php echo $r['fltno'];?>" class="form-control" readonly="readonly" /></td>
        <td>
            <select name="opname[]" class="form-control js-sel-box" data-id="<?php echo $c;?>">
                <option>Select...</option>
                <?php  
                    $fs = getOperators($_SESSION['location']); 
                    while($f = mysqli_fetch_array($fs)){
                ?>
                        <option value="<?php echo $f['name'];?>"><?php echo $f['name'];?></option>
                <?php 
                    };
                ?>
            </select>
        </td>
        <td class="js-sel-box-ajax-result-<?php echo $c;?>"></td>
    <?php 
        }//End While
    ?>
    </tr>
    

    Ajax Part:

    <script type="text/javascript">
    $(document).ready(function() {
        $(".js-sel-box").change(function(){
            var sname = $(this).val();
            var result_id = $(this).attr("data-id");
            $.ajax({
                type:"POST",
                url:"process-opno.php",
                data:{opname:sname}
            }).done(function(data3){
                $(".js-sel-box-ajax-result-"+result_id).html(data3);  
            });
        });
    });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思