douba3943
douba3943
2015-12-26 11:17

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

  • php
  • mysql
  • ajax
  • 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条回答

为你推荐