drp935159 2016-06-28 11:17
浏览 53
已采纳

仅从一个按钮提交多个值

Hi i am trying to submit the multiple form values only from one button i have 10 records and one record has one button of submit and 10 records have 10 buttons of submit what i want there should be only one button of submit and i can submit he whole 10 records from one button:

 echo "<table class='table table-hover table-responsive table-bordered'>";

// our table heading
echo "<tr>";
echo "<th class='textAlignLeft'>Product Name</th>";
echo "<th>Price</th>";
echo "<th style='width:5em;'>Quantity</th>";
echo "<th>Image</th>";
echo "<th>Action</th>";


echo "</tr>";

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    extract($row);


    //creating new table row per record
    echo "<tr>";
    echo "<td>";
    echo "<div class='product-id' style='display:none;'>{$id}</div>";
    echo "<div class='product-name'>{$name}</div>";
    echo "</td>";
    echo "<td>&#36;" . number_format($price, 2, '.' , ',') . "</td>";
    if(isset($quantity)){
        echo "<td>";
        echo "<input type='text' name='quantity' value='{$quantity}' disabled class='form-control' />";
        echo "</td>";

        echo "<td>";
        echo "<td>";
        echo "<button class='btn btn-success' disabled>";
        echo "<span></span> Submitted!";
        echo "</button>";
        echo "</td>";
        echo "</td>";
    }else{
        echo "<td>";
        echo "<input type='number' name='quantity[]' value='1' class='form-control'/>";
        echo "</td>";
        echo "<td><img src='product-images/{$image}' width='60' height='60'</td>";
        echo "<td>";
        echo "<button class='btn btn-primary add-to-cart'>";
        echo "<span></span>Submit to cart";
        echo "</button>";
        echo "</td>";
    }

    echo "</tr>";
}

echo "</table>";

} what i want only one button from i can submit all my values: Any help would be high appreciated:

     $action = isset($_GET['action']) ? $_GET['action'] : "";
     $product_id = isset($_GET['product_id']) ? $_GET['product_id'] : "1";
     $name = isset($_GET['name']) ? $_GET['name'] : "";
      $quantity = isset($_GET['quantity']) ? $_GET['quantity'] : "1";
     $image = isset($_GET['image']) ? $_GET['image'] : "1";


      if($action=='added'){
          echo "<div class='alert alert-info'>";
         echo "<strong>{$name}</strong> added to your cart!";
          echo "</div>";
        }

       else if($action=='failed'){
          echo "<div class='alert alert-info'>";
           echo "<strong>{$name}</strong> failed to add to your cart!";
          echo "</div>";
            }

             // left join to select products
              $query = "SELECT p.id, p.name, p.price, p.image, ci.quantity 
            FROM products p 
             LEFT JOIN cart_items ci
            ON p.id = ci.product_id 
         ORDER BY p.name";

         $stmt = $con->prepare( $query );
        $stmt->execute();

       // count number of products returned
         $num = $stmt->rowCount();







          HTML CODE 

                                  <?php
       // connect to database
    include 'config/db_connect.php';

     // page headers
      $page_title="Cart";
  include 'layout_head.php';

      // parameters
      $action = isset($_GET['action']) ? $_GET['action'] : "";
  $name = isset($_GET['name']) ? $_GET['name'] : "";
   $image = isset($_GET['image']) ? $_GET['image'] : "";

       // display a message
    if($action=='removed'){
       echo "<div class='alert alert-info'>";
        echo "<strong>{$name}</strong> was removed from your cart!";
       echo "</div>";
   }

      else if($action=='quantity_updated'){
       echo "<div class='alert alert-info'>";
      echo "<strong>{$name}</strong> quantity was updated!";
      echo "</div>";
    }

   else if($action=='failed'){
      echo "<div class='alert alert-info'>";
     echo "<strong>{$name}</strong> quantity failed to updated!";
       echo "</div>";
     }

     else if($action=='invalid_value'){
         echo "<div class='alert alert-info'>";
          echo "<strong>{$name}</strong> quantity is invalid!";
         echo "</div>";
        }

    // select products in the cart
             $query="SELECT p.id, p.name, p.price, p.image, ci.quantity, ci.quantity *    p.price AS subtotal  
        FROM cart_items ci  
            LEFT JOIN products p 
                ON ci.product_id = p.id";

     $stmt=$con->prepare( $query );
    $stmt->execute();

 // count number of rows returned
  $num=$stmt->rowCount();

        if($num>0){

          //start table
       echo "<table class='table table-hover table-responsive table-bordered'>";

           // our table heading
          echo "<tr>";
        echo "<th class='textAlignLeft'>Product Name</th>";
          echo "<th>Price</th>";
           echo "<th style='width:15em;'>Quantity</th>";
         echo "<th>Sub Total</th>";
       echo "<th>Action</th>";
         echo "</tr>";

       $total=0;

     while( $row = $stmt->fetch(PDO::FETCH_ASSOC)){
       extract($row);

       echo "<tr>";
       echo "<td>";
       echo "<div class='product-id' style='display:none;'>{$id}</div>";
       echo "<div class='product-name'>{$name}</div>";
       echo "</td>";
       echo "<td>&#36;" . number_format($price, 2, '.', ',') . "</td>";
       echo "<td>";
       echo "<div class='input-group'>";
       echo "<input type='number' name='quantity[]' value='{$quantity}'   class='form-control'>";

       echo "<span class='input-group-btn'>";
      echo "<button class='btn btn-default update-quantity'      type='button'>Update</button>";
        echo "</span>";

        echo "</div>";
        echo "</td>";
        echo "<td>&#36;" . number_format($subtotal, 2, '.', ',') . "</td>";
        echo "<td>";
             echo "<a href='remove_from_cart.php?id={$id}&name={$name}'     class='btn     btn-danger'>";
         echo "<span></span> Delete from cart";
        echo "</a>";
           echo "</td>";
          echo "</tr>";

         $total += $subtotal;
        }

       echo "<tr>";
          echo "<td><b>Total</b></td>";
        echo "<td></td>";
           echo "<td></td>";
         echo "<td>&#36;" . number_format($total, 2, '.', ',') . "</td>";
      echo "<td>";
     echo "</a>";
     echo "</td>";
       echo "</tr>";

   echo "</table>";
        }else{
      echo "<div class='alert alert-danger'>";
      echo "<strong>No products found</strong> in your cart!";
         echo "</div>";
        }


         include 'layout_foot.php';
          ?>
  • 写回答

1条回答 默认 最新

  • 普通网友 2016-06-28 11:38
    关注

    Use name quantity[] instead of quantity.By this way you can receive post data as an array. After the loop use only one submit button.

    echo "<input type='number' name='quantity[]' value='1' class='form-control'/>";
    

    Then receive it by using foreach

    foreach($_POST['quantity'] as $qty)
    {
       echo $qty;
       //Here you will receive all the quantities one by one.
    }
    

    Form the code

    <?php
    
     echo "<table class='table table-hover table-responsive table-bordered'>";
    
    // our table heading
    echo "<tr>";
    echo "<th class='textAlignLeft'>Product Name</th>";
    echo "<th>Price</th>";
    echo "<th style='width:5em;'>Quantity</th>";
    echo "<th>Image</th>";
    
    
    echo "</tr>";
    
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row);
    
    
        //creating new table row per record
        echo "<tr>";
        echo "<td>";
        echo "<div class='product-id' style='display:none;'>{$id}</div>";
        echo "<div class='product-name'>{$name}</div>";
        echo "</td>";
        echo "<td>&#36;" . number_format($price, 2, '.' , ',') . "</td>";
        if(isset($quantity)){
            echo "<td>";
            echo "<input type='text' name='quantity[]' value='{$quantity}' disabled class='form-control' />";
            echo "</td>";
    
            echo "<td>";
            echo "</td>";
            echo "<td>";
            echo "</td>";
        }else{
            echo "<td>";
            echo "<input type='number' name='quantity[]' value='1' class='form-control'/>";
            echo "</td>";
            echo "<td><img src='product-images/{$image}' width='60' height='60'</td>";
            echo "<td>";
            echo "</td>";
        }
    
        echo "</tr>";
    }
        echo "<tr><td colspan='4'>";
            echo "<button class='btn btn-primary add-to-cart' type='submit'>";
            echo "<span></span>Submit to cart";
            echo "</button>";
        echo "</td></tr>";
    
    echo "</table>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失