doujiyan0031 2019-06-24 10:01
浏览 64

使用jquery ajax动态添加输入字段

I'm trying to inserting multiple items connected with quantity and price for each. all of the items will be showing the same invoice number and vendor id.

<form name="order" id="order">

<label>Invoice Number</label>
    <input type="text" name="invoicenumber" value="">
<p><label>Vendor's ID</label>
<select name="vendorid" >

<?php

    $stmt = $dbcon->prepare("SELECT * FROM customers");
    $stmt->execute(); 
    while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
        extract($row);
        ?>

        <option value=" <?php echo $id; ?> "><?php echo $vendorname; ?></option>

<?php
    } ?>
    </select> </p>

<table class="table table-bordered" id="dynamic_field">

    <!-- Product code begining -->

    <td><tr><label>Items sold</label>
    <select name="proid[]" >

<?php

    $stmt = $dbcon->prepare("SELECT * FROM products");
    $stmt->execute(); 
    while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
        extract($row);
        ?>

        <option value=" <?php echo $pid; ?> "><?php echo $pname; ?></option>

<?php
    } ?>
    </select> </td>

    <td><label>Quantity</label>
    <input type="text" name="tquantity[]" value=""></td>

    <td><label>Price</label>
    <input type="text" name="saleprice[]" value=""></td>

    <td><button name="add" id="add" class="btn btn-success">ADD MORE</button></td></tr>
    </table>
    <!-- Product code END -->


    <input type="submit" name="submit" value="submit">
</form>

<!--Jquery code to add more fields -->
<script>
$(document).ready(function(){
    var i = 1;
    $(#add).click(function(){
        i++;
        $(#dynamic_field).append('<tr id="row'+i+'"><td>
    <select name="proid[]" >

<?php

    $stmt = $dbcon->prepare("SELECT * FROM products");
    $stmt->execute(); 
    while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
        extract($row);
        ?>

        <option value=" <?php echo $pid; ?> "><?php echo $pname; ?></option>

<?php
    } ?>
    </select> </td>

    <td><label>Quantity</label>
    <input type="text" name="tquantity[]" value=""></td>

    <td><label>Price</label>
    <input type="text" name="saleprice[]" value=""></td>

    <td><button name="remove" id="remove" class="btn btn-danger btn_remove">Remove</button></td></tr>');
    });
    $(document).on('click','.btn_remove',funcion(){
        var button_id= $(this).attr("id");
        $("#row"+button_id+"").remove();
    });
    $(#submit).click(function(){
        $.ajax({
        url:"processinvoice.php",
        method:"POST",
        data:$('#order').serialize(),
        success:function(data){
            alert(data);
            $('#order')[0].reset();
        }
        });
    });
}
</script>

So I'm trying to add input fields dynamically using jQuery ajax following a tutorial but i can't seem to make it work

When I click add to add fields only the link changes to /admin/addinvoice.php?invoicenumber=&vendorid=+2+&proid[]=+10+&tquantity[]=&saleprice[]=&add= and it's not adding a field

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看