how to not trigger all submit button in while loop when submit button isset

时间:2018-05-14 17:29:23

标签: php

<?php while($row = mysqli_fetch_assoc($res)): ?>
  <form method="post">
    <?php

      $item_id = $row['item_id'];
      $span    = $row['auction_span'];
      $seller_id= $row['seller_id'];

      // publish auction
      if(isset($_POST['publish']))  {

        $hidden     = $_POST['hidden'];
        $auction_id = uniqid("A");
        $exp        = 0; 
        $datePub    = date("F/d/Y H:i:s");
        $dateExp    = date("F/d/Y H:i:s", strtotime('+' . $span . ' days'));

        $update="UPDATE items SET status=1 where item_id='$hidden'";
        mysqli_query($conn,$update);                        

        $insert="INSERT INTO auction (auction_id,item_id,seller_id,datePub,
                                      dateExp,expired)
                 VALUES ('$auction_id','$item_id','$seller_id','$datePub',
                         '$dateExp','$exp')";

        mysqli_query($conn,$insert);

      }

    ?>
    <div class="float-l">
        <p><?php echo $row['item_name'];?></p>
        <p><?php echo $row['item_price'];?></p>
        <p><?php echo $row['item_conditon'];?></p>
        <p><?php echo $row['item_description'];?></p>
        <p><?php echo $row['seller_id'];?></p>
    </div>
    <div class="float-l">
        <input type="hidden" name="hidden" value="<?php echo $item_id;?>">
        <input type="submit" name="publish" value="Publish">
    </div>
  </form>
<?php   endwhile; ?>

I have too many submit button that has the same name displaying because of the while loop. When I click the publish button of one item only, It triggers all the publish button and the other items that i did not publish will be published and inserted into the auction table however the UPDATE query is not affected it only update 1 row in the table and is working fine. How can I click one submit button that will not affect any other submit button and will only insert 1 data in the table?

1 个答案:

答案 0 :(得分:0)

如果您确实需要向页面添加多个表单,可以使用Javascript助手提交指定的表单。

Javacript:

<script>
function triggerFormSubmit(form) {
    document.forms[form].submit();
}
</script>

在html代码中:

表格

<form method="post" id="<?php echo $row['item_id'] ?>">

提交按钮:

<input name="publish" value="Publish" onclick="triggerFormSubmit("<?php echo $row['item_id'] ?>");">