dongmeng2687 2017-11-02 11:01 采纳率: 0%
浏览 29
已采纳

无法访问POST中所有动态添加的表元素

I am working on a web based erp kinda platform but stuck at this situation where I want to save all the data from some textbox and select tags than save them all to the database.

This is the a simple table. After the page finishes loading by default there is one row for inserting data. But if (and only if) the buyer wants to buy some more material than I want the user to insert more row and save all the data to the db.

<form method="post" style="border:1px solid #ccc">
<div class="container"><label><b>Billing details</b></label><table border='1' id="myTable" class="myTable">`<form method="post" style="border:1px solid #ccc">
  <div class="container">
    <label><b>Billing details</b></label>
    <table border='1' id="myTable" class="myTable">
     
      <thead>
       <tr>
        <th>Sr. No</th>
        <th>Particulars</th>
        <th>QTY</th>
        <th>RATE</th>
        <th>DISCOUNT</th>
        <th id="gt">AMOUNT</th>
       </tr>
     </thead>
     <tbody>
      <tr id="content">
        <td></td>
        <td><?php echo $select; ?></td>
        <td><input type="text" name="qty" id="qty" class="qty" onKeyPress="sum()"/></td>
        <td><input type="text" name="rates" id="rates" class="rates" onKeyPress="sum()"/></td>
        <td><input type="text" name="dis" placeholder="0" class="dis" id="dis" onKeyPress="sum()"/></td>
        <td><input type="text" name="amount" readonly class="amount" id="amount"/></td>
      </tr>
     </tbody> 
    
    </table>
    <div><b>Total Amount</b><b><input type="text" name="total" id="gTotal" class="gTotal" readonly/></b></div><br/>
    <div><b>Paid Amount</b><input type="text" name="paidamount" class="paidamount" id="paidamount"/></div><br/>
    <div><b>Balance Amount</b><input readonly type="text" name="balance" class="balance" id="balance"/></div><br/>
    <div id="addrow" style="width:50px;float: left;" onClick="addRow()">+</div>
    <div id="remrow" style="width:50px;float: left;" onClick="remRow()">-</div>
    
    <div class="clearfix">
    <input class="signupbtn" type="submit" name="submit" value="Register"/>  
    </div>
  </div>
</form>

The PHP Part

include "dbconnection.php";
$sql=mysqli_query($connection,"SELECT id,name FROM materials");

if(mysqli_num_rows($sql)){

$select= '<select class="mater" name="mate">';
while($rs=mysqli_fetch_array($sql)){

      $select.='<option value='.$rs['name'].'>'.$rs['name'].'</option>';
  } 
} 
$select.='</select>';

 mysqli_close($connection);

 if(isset($_POST['submit'])){

    print_r($_POST['mate']);
    echo count($_POST['mate']);

    foreach($_POST as $name => $item){

        echo count($_POST['mate']);

        if($name != 'submit'){
            for($m=0; $m < sizeof($item); $m++){
               echo ($name.' '.$item[$m].'<br>');
               //Insert something in db
            }
        }
    } 
}

The print_r($_POST['mate']); prints the value selected in the last row ( for more than one row) or value in the first row (if user does not add additional row). This is my table structure

 id name address    mobile  material    qty discount    total   balance 

The sizeof($item) or count() returns 1 even after adding one more row. I have tried messing up with something like [0], but got confused. I am not well with php and this is my first project related to the dbs.

What I want is when user clicks on register, all the data available in the text box and the select element gets save in the db. Each table row should be saved to one separate row. I have searched a lot but could not get it working.

</div>
  • 写回答

1条回答 默认 最新

  • doujionggan9570 2017-11-02 11:12
    关注

    You can give name like mate[] for each select element inclusding dynamically created element. So in post data you will get array named mate.

    <select class="mater" name="mate[]">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀