doumengyin0491 2017-05-09 03:29
浏览 566
已采纳

如何用php追加HTML表格?

I'm trying to create a simple Point of sale using PHP, and I want to add data in my table everytime I search and submit the barcode. The problem with my code, it only runs once, it appends the first data I add but for the next nothing happens, here is my HTML

<form class="" action="" id="pos_data" method="post">
    <input type="text" name="txtSearch"  placeholder="barcode" autofocus>
</form>

<table id="pos-items">
    <thead><tr>
        <td>Product Name</td>
        <td>Quantity</td>
        <td>Unit</td>
        <td>Price</td>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

and this my script

$('#pos_data').submit(function() {
$.ajax({
    url: 'processpos.php',
    type: 'POST',
    dataType: 'html',
    data: $(this).serialize(),
    success: function(newContent) {
    $('#pos-items tbody').append(newContent);
          }
    });
return false;
barcodeenter();
});

function barcodeenter(){

document.querySelector('#txtSearch').addEventListener('keypress', function (e){
    var key = e.which || e.keyCode;
    if (key === 13) { // 13 is enter
      console.log(document.getElementById("txtSearch").value);
      document.getElementById("txtSearch").value = "";

    }
    });
}

and my PHP file

include 'conn_db.php';
$product_id = $_POST['txtSearch'];


$sql = "SELECT * FROM producttbl WHERE product_id ='".$product_id."'";
$records = mysqli_query($conn, $sql);



while($row = mysqli_fetch_assoc($records)){

    echo "<tr><td>".$row['product_name']."</td><td>1</td><td>".$row['product_unit']."</td><td>".$row['product_price']."</td></tr>";
}

all in the code works for the first product but when i try to add another nothing happens. Is there anything I'm missing out here?

  • 写回答

4条回答 默认 最新

  • dongtun3328 2017-05-09 05:08
    关注

    Remove the function barcodeenter()

    and replace with document.getElementById("pos_data").reset();

     $('#pos_data').submit(function() {
                    $.ajax({
                        url: 'processpos.php',
                        type: 'POST',
                        dataType: 'html',
                        data: $(this).serialize(),
                        success: function(newContent) {
                            $('#pos-items tbody').append(newContent);
                        }
                    });
                    //barcodeenter();
                    document.getElementById("pos_data").reset();
                    return false;
                });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 提问应符合社区要求 10月9日

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示