douba3943 2015-12-26 11:17
浏览 45
已采纳

将Mysql数据库中的记录提取到jquery脚本中

Am adding new table row using onclick function. I want to get records from MySql into a select option tag when a new row is added.

var i=$('table tr').length;
$(".add").on('click',function(){
    html = '<tr>';
    html += '<td><input class="case" type="checkbox"/></td>';
    html += '<td> <select id="credit_1" name="account_code[]" class="form-custom-control" style="padding-top:0px;"><option></option></select></td>';
    html += '<td>
                 <select name="fund_id[]" class="form-custom-control">
                     <option></option>
                </select>
             </td>';
    html += '<td><input type="text" name="category[]" id="category_'+i+'"></td>';
    html += '</tr>';
    $('table').append(html);
    i++;
});

Ajax

$(function(){
  $(document).on("click",".add",function(e){
    e.preventDefault();
    var _this=$(this);
    var id =_this.val();
    var fund=_this.closest("tr").find("select[name='fund_id[]']");
    var dataString = 'id='+ id;
  $.ajax({
            type: "POST",
            url: "populate_fund_type.php",
            data: dataString,
            cache: false,
            success: function(html)
            {
               fund.html(html);
           } 
      });
  });
});

Php

     $id = $_POST['id'];
    $sql = $link->query("SELECT * FROM funds");
     echo '<option></option>';
    while($row = mysqli_fetch_array($sql)) {
   echo '<option value="'.$row['fund_id'].'">'.$row['fund_name'].'</option>'; 
   }

Any help will be appreciated. Thanks

  • 写回答

1条回答 默认 最新

  • duanmei2805 2015-12-26 12:01
    关注

    you can use ajax and jquery for this.and on success append html into the div or table. ajax code

    $.ajax({
        type: 'POST',
        url: 'yourphp_page.php',
        data: {status:"0"}
    }).done(function(html){     
    $('#yourtable_id').append(html);
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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