Ajax在我的表的第二行中不起作用

时间:2015-01-26 08:20:05

标签: php jquery mysql ajax

我有一个填充项目列表的表格,当我从列表中选择一个项目时,它会从数据库中将价格提取到输入字段中。这里的问题是,只有表的第一行有效,当我从第二行中选择时,它根本不起作用。感谢

表格

<div class="widget-content no-padding">
  <table class="table table-hover table-striped table-bordered table-highlight-head" id="sample_tbl">
    <thead>
      <tr>
        <th>Item name</th>
        <th>Quantity</th>
        <th>Price</th>
        <th>Amount</th>
        <th>Description</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="col-md-3">
          <select name="item_name" class=" select2 form-control" required="required" id="item_name">
            <option></option>
            <?php $u_qry=$ link->query("select * from items"); while($list = mysqli_fetch_array($u_qry)) { echo"
            <option value=\ "{$list['items_code']}\">{$list['item_name']}</option>"; } ?>
          </select>
        </td>
        <td>
          <input type="text" class="form-control">
        </td>
        <td>
          <input type="text" class="form-control" id="price">
        </td>
        <td>
          <input type="text" class="form-control">
        </td>
        <td>
          <input type="text" class="form-control">
        </td>
      </tr>
      <tr>
        <td class="col-md-3">
          <select name="item_name" class=" select2 form-control" required="required" id="item_name">
            <option></option>
            <?php $u_qry=$ link->query("select * from items"); while($list = mysqli_fetch_array($u_qry)) { echo"
            <option value=\ "{$list['items_code']}\">{$list['item_name']}</option>"; } ?>
          </select>
        </td>
        <td>
          <input type="text" class="form-control">
        </td>
        <td>
          <input type="text" class="form-control" id="price">
        </td>
        <td>
          <input type="text" class="form-control">
        </td>
        <td>
          <input type="text" class="form-control">
        </td>
      </tr>
    </tbody>
  </table>
</div>

AJAX

<script type="text/javascript">
  $(document).ready(function() {
    $("#item_name").change(function() {
      var item = $("#item_name").val();
      $.ajax({
        type: "post",
        url: "auto_item_price.php",
        data: "item_name=" + item,
        success: function(data) {
          console.log(data);
          $("#price").val(data);
        }
      });
    });
  });
</script>

PHP

<?php
    include("includes/db_connect.php");
    $item_id = $_POST['item_name'];
    $sql = $link->query("SELECT * FROM items WHERE items_code = $item_id");
    while($row = mysqli_fetch_array($sql)) {
        echo "{$row['purchase_price']}";
    }
?>

0 个答案:

没有答案