仅从一个按钮提交多个值

时间:2016-06-28 11:17:46

标签: php

您好我试图仅从一个按钮提交多个表单值我有10个记录和一个记录有一个提交按钮和10个记录有10个按钮提交我想要的应该只有一个提交按钮我可以从一个按钮提交他整整10条记录:

 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>";

}    我想要的只有一个按钮我可以提交我所有的价值观:任何帮助都会受到高度赞赏:

     $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 个答案:

答案 0 :(得分:0)

使用名称数量[]而不是数量。通过这种方式,您可以将数据作为数组接收。循环后只使用一个提交按钮。

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

然后使用foreach

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

形成代码

<?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>";