在php中保存动态添加的文本框

时间:2013-12-09 15:00:29

标签: php mysql dynamic tablerow

您正在处理一个能够在按钮单击时添加表行的表单,但问题是我的php for循环正在保存第一行而不保存其他textboxe值。我卡住了。这是我的代码

<form class="form-horizontal" name="invoice" method="POST">
        <div id="div-2" class="accordion-body collapse in body">
                <div style="max-height:300px;overflow: auto; border: 0px solid;">
                <div style="float:left; margin-right:-8220000px;">   
            <table id="data" border="1px" width="90%">

                <tr>
                    <td width="60px" align="center"> <label class="control-label">Sr No.</label></td>
                    <td width="250px" align="center"><label class="control-label">Item Name</label></td>
                    <td width="90px" align="center"><label class="control-label">Qty</label></td>
                    <td width="50px" align="center"><label class="control-label">Unit</label></td>
                    <td width="100px" align="center"><label class="control-label">Rate</label></td>
                    <td width="120px" align="center"><label class="control-label">Amount</label></td>
                    <td width="200px" align="center"><label class="control-label">Marks& Nos</label></td>
                    <td width="250px" align="center"><label class="control-label">Description</label></td>
                </tr>
                    <tr>
                        <td align="left"><input type="text" size="6" maxlength="6" maxlength="6" name="srno_0" class="sno form-control"/></td>
                        <td align="left">
                            <select data-placeholder="Item"  name="item_0" class="form-control"  >
                                <option value=""></option>
                                <?php
                                    $mown = mysql_query("SELECT code, `name` FROM `item`") or die(mysql_error());
                                    while ($trow = mysql_fetch_array($mown)) {
                                           echo "<option value=$trow[code]>$trow[name]</option>";
                                    }
                                ?>
                            </select>
                        </td>
                        <td align="center"><input type="text" size="6" maxlength="9" maxlength="6" name="qty_0" class="qty form-control" style='text-align:right'/></td>
                        <td align="left">
                            <input type="text" size="6" maxlength="6" name="unit_0" class="unit form-control"/>
                        </td>
                        <td align="center"><input type="text" size="6" maxlength="9" maxlength="6" name="rate_0" class="rate form-control" style='text-align:right'/></td>
                        <td align="center"><input type="text" size="6" maxlength="9" maxlength="6" name="amt_0" id="amount" class="amt form-control" style='text-align:right' readonly/></td>
                        <td align="center"><input type="text" size="6"  name="marks_0" class="trip form-control"/></td>
                        <td align="center"><textarea name="descrip_0" cols="90" rows='1' class="desc form-control"></textarea></td>

                    </tr>
   </table>
            </div>  

    </div>
            <table border='0' width='100%'>


        <tr>
            <td width="150px">
                <input type="button" id="addnew" class="btn btn-success btn-lg" name="addnew" value="+" />
                       <input type="hidden" id="items" name="items" value="<?php echo ($addflag == 0) ? $ctr : 1; ?>" />  
</td><td width="240px"></td><td width="140px"></td><td width="200px" align="right"><label for=""><font color="black" size="3px">SubTotal</font><span></span></label></td>
            <td width="200px"><input id="total" type="text" name="total" class="form-control"  value="<?php echo ($addflag == 0) ? $get['total'] : "0.00"; ?>" readonly style='text-align:right'></td>
        </tr>
    here is my jquery code
  <script type='text/javascript'>
            $(document).ready(function() {
                $('#data').on('keyup', '.qty, .rate, .amt', calculateRow);
                $('#data').on('blur', '.desc', addrows);

                  function addrows(){    

                   var ctr = $('#items').val();
                    $.post('bmt_invsrch.php', {ctr: ctr}, function(data) {
                        $(data).appendTo('#data');
                        ctr++;
                        $('#items').val(ctr);
                    });
                }

                $('#addnew').click(function() {
                       addrows();
                });
            });


        </script>

这是我的php

for ($i = 0; $i<$count; $i++){
    $msrno         = $_POST["srno_$i"];
    $mitem         = $_POST["item_$i"];
    $mqty          = $_POST["qty_$i"];
    $munit         = $_POST["unit_$i"];
    $mrate         = $_POST["rate_$i"];
    $mamt          = $_POST["amt_$i"];
    $mmarks        = $_POST["marks_$i"];
    $mdescrip      = $_POST["descrip_$i"];

         mysql_query("INSERT INTO `saletran`(vouchno, srno, item, qty, unit, rate, amt, marks, descrip)
                     VALUES('$mvouch', '$msrno', '$mitem', '$mqty', '$munit', '$mrate', '$mamt', '$mmarks', '$mdescrip')") or die(mysql_error());



}

1 个答案:

答案 0 :(得分:0)

您可以将输入字段分组,例如

<form method='post'>
  <input type='text' name='test[]' />
  <input type='text' name='test[]' />
  <input type='text' name='test[]' />
  <input type='text' name='test[]' />

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

当您提交此表单时,结果将类似于

Array
(
  [test] => Array
      (
          [0] => 21
          [1] => 212
          [2] => 121
          [3] => 21
      )

  [submit] => submit
)