一键提交循环生成的多种表单

时间:2019-03-04 08:13:28

标签: javascript php jquery html forms

对不起,意大利面条代码:(我不知道为什么它不起作用。Button只提交最后一张表格,但是我想提交所有生成的表格。我该怎么做?我尝试了AJAX,但给出了相同的结果我想递归地添加选择,但是我认为这是最困难的方法,还有另一种解决方案。

 <table id="datatable" class="table table-striped table-bordered">
   <thead>
    <tr>
     <th>Product Name</th>
     <th>Product Weight</th>
     <th>Product Image</th>
     <th>Status</th>
     <th>Choose suplier</th>
     <th>Delete</th>
    </tr>
   </thead>
   <tbody>
   <?php        
   foreach ($result as $k=>$v)
   {
      $oid[] = $v['op_id'];
      $pui[] = $v['pui'];
      ?>
      <tr>
       <td><?php echo substr($v['name'], 0,10); ?></td>
       <td><?php echo substr($v['gram'], 0,10); ?></td>
       <td><img style="width: 50%" src="<?php echo $site_url.'images/'.$v['image']; ?>" alt=""></td>
       <td><?php echo substr($v['opia'], 0,10); ?></td>
       <td>
          <form method="POST" enctype="multipart/form-data" id="demo-form2" data-parsley-validate oidd="<?php echo $v['pui']; ?>" class="form-horizontal form-label-left sels<?php echo $v['op_id'];?>">
           <div class="form-group">
             <label class="">
             </label>
           <div class="" style="width: 100%">
           <?php
            $sups = $db->prepare('SELECT * FROM product p, suplier s, prod_suplier ps WHERE p.u_id=ps.p_id AND ps.suplier_id=s.id AND p.u_id=:u_id GROUP BY s.id');
            $sups->execute(array('u_id'=>$v['pui']));
            $count_sups = $sups->rowCount();
            if ($count_sups==1) {
              $get_sups = $sups->fetch(PDO::FETCH_ASSOC);
              echo $get_sups['full_name'];
            } else {
            ?>
              <select style="" name="supliers" id="">
               <?php
                while ($get_sup=$sups->fetch(PDO::FETCH_ASSOC)) {
                ?>
                 <option value="<?php echo $v['sfn']; ?>"><?php echo $get_sup['full_name']; ?></option>
          <?php }?>
              </select>
      <?php } ?>
           </div>
         </div>
       </form>
      </td>
      <td>
       <form style=" float: right;" method="POST" action="<?php echo $site_url.$state.'delete/' ?>">
       <input type="hidden" name="delid" value="<?php echo $v['oid'] ?>">
       <button type="button" onclick="dsomo(this);" name="dbtn" style="color: red; background-color: rgba(0,0,0,0); border:none;" href="">
       <i class="fa fa-trash fa-2x" ></i>
       </button>
       </form>
      </td>
     </tr>
<?php }?>
    </tbody>
   </table>
  </div>
 </div>
</div>
<button type="submit" id="acception" name="submit">submit</button>
<script async>
$('#acception').on('click', function () {
<?php
for ($i=0;$i<count($oid);$i++) {
?>
  $("tr>td>.sels<?php echo $oid[$i];?>").submit();
<?php } ?>
})
</script>

1 个答案:

答案 0 :(得分:0)

感谢MojoAllmighty,我找到了解决方案。如下面的链接所述,如果我想通过一个按钮提交多个表单,则必须为表单分配相等的类,并使用ajax异步提交表单。因此,也许可以将其标记为下面链接的重复。