How do I stop user from leaving quantity empty?

Viewed 60

enter image description here

So this is my order page and I want to add an option which prevents the user from leaving the quantity option empty. I cannot seem to use the required option as it compels the user to fill every box instead of the one just selected.

Here is the code

<body>

   <?php include('navbar.php'); ?>
   <div class="container">
       <h1 class="page-header text-center">ORDER</h1>
       <form method="POST" action="purchase.php">
           <table class="table table-striped table-bordered">
               <thead>
                   <th class="text-center"><input type="checkbox" id="checkAll"></th>
                   <th class="productheading">Category</th>
                   <th class="productheading">Product Image
                   <th class="productheading">Product Name</th>
                   <th class="productheading">Price</th>
                   <th class="productheading">Quantity</th>
               </thead>
               <tbody>
                   <?php
                   $sql = "select * from product left join category on category.categoryid=product.categoryid order by product.categoryid asc, productname asc";
                   $query = $conn->query($sql);
                   $iterate = 0;
                   while ($row = $query->fetch_array()) {
                   ?>
                       <tr>
                           <td class="text-center"><input type="checkbox" value="<?php echo $row['productid']; ?>||<?php echo $iterate; ?>" name="productid[]" style=""></td>
                           <td><?php echo $row['catname']; ?></td>

                           <td><a href="<?php if (empty($row['photo'])) {
                                               echo "upload/noimage.jpg";
                                           } else {
                                               echo $row['photo'];
                                           } ?>"><img src="<?php if (empty($row['photo'])) {
                                                               echo "upload/noimage.jpg";
                                                           } else {
                                                               echo $row['photo'];
                                                           } ?>" height="170px" width="80%"></a></td>
                           <td class="productname1"><?php echo $row['productname']; ?></td>
                           <td class="price">Rs <?php echo number_format($row['price'], 2); ?></td>
                       <!-->**HERE IS THE CODE THAT NEEDS TO BE FIXED**--> <td><input type="number" class="form-control" name="quantity<?php echo $iterate; ?>"></td>
                       </tr>
                   <?php
                       $iterate++;
                   }
                   ?>
               </tbody>
           </table>

           <div class="row">
               <div class="col-md-3">
                   <input type="text" name="customer" class="form-control" placeholder="Customer Name" required>
               </div>
               <div class="col-md-3">
                   <input type="number" name="number" class="form-control" placeholder="Contact Number" required>
               </div>
               <div class="col-md-2" style="margin-left:-20px;">
                   <button type="submit" onclick="myFunction()" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Order</button>
                   <br />
                   <br />
                   <br />

               </div>
           </div>
       </form>
   </div>
   <script type="text/javascript">
       $(document).ready(function() {
           $("#checkAll").click(function() {
               $('input:checkbox').not(this).prop('checked', this.checked);
           });
       });
   </script>
</body>

</html>
2 Answers

You can target those fields with jQuery/JavaScript and make it required by focusing on it and then prevent the form from submitting. Try this

$(document).ready(function() {
    $('#order-form').submit(function(e){
        let $quantities = $(this).find('.table input[type="number"]').filter(function(){
            return $(this).closest('tr').find('input[type="checkbox"]').is(':checked') && $(this).val() === '';
        })
        if( $quantities.length > 0 ){
            e.preventDefault();
            $quantities.first().focus()
        }
    })
});
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="container">
   <form method="POST" action="purchase.php" id="order-form">
       <table class="table table-striped table-bordered">
           <thead>
               <th class="text-center"><input type="checkbox" id="checkAll"></th>
               <th class="productheading">Category</th>
               <th class="productheading">Product Image
               <th class="productheading">Product Name</th>
               <th class="productheading">Price</th>
               <th class="productheading">Quantity</th>
           </thead>
           <tbody>
               <tr>
                   <td class="text-center"><input type="checkbox" value="1" name="productid[]" checked></td>
                   <td>Dishes</td>
                   <td><a href=""><img src="https://via.placeholder.com/150/0000FF/FFFFFF?text=Bara" height="170px" width="80%"></a></td>
                   <td class="productname1">Bara</td>
                   <td class="price">Rs 79.00</td>
                   <td><input type="number" class="form-control" name="quantity[]"></td>
               </tr>
               <tr>
                   <td class="text-center"><input type="checkbox" value="1" name="productid[]"></td>
                   <td>Dishes</td>
                   <td><a href=""><img src="https://via.placeholder.com/150/FF0000/FFFFFF?text=Chwela" height="170px" width="80%"></a></td>
                   <td class="productname1">Chwela</td>
                   <td class="price">Rs 120.00</td>
                   <td><input type="number" class="form-control" name="quantity[]"></td>
               </tr>
           </tbody>
       </table>
        <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Order</button>
   </form>
</div>

Answer in format requested by the OP

<body>

   <?php include('navbar.php'); ?>
   <div class="container">
       <h1 class="page-header text-center">ORDER</h1>
       <form method="POST" action="purchase.php" id="order-form">
           <table class="table table-striped table-bordered">
               <thead>
                   <th class="text-center"><input type="checkbox" id="checkAll"></th>
                   <th class="productheading">Category</th>
                   <th class="productheading">Product Image
                   <th class="productheading">Product Name</th>
                   <th class="productheading">Price</th>
                   <th class="productheading">Quantity</th>
               </thead>
               <tbody>
                   <?php
                   $sql = "select * from product left join category on category.categoryid=product.categoryid order by product.categoryid asc, productname asc";
                   $query = $conn->query($sql);
                   $iterate = 0;
                   while ($row = $query->fetch_array()) {
                   ?>
                       <tr>
                           <td class="text-center"><input type="checkbox" value="<?php echo $row['productid']; ?>||<?php echo $iterate; ?>" name="productid[]" style=""></td>
                           <td><?php echo $row['catname']; ?></td>

                           <td><a href="<?php if (empty($row['photo'])) {
                                               echo "upload/noimage.jpg";
                                           } else {
                                               echo $row['photo'];
                                           } ?>"><img src="<?php if (empty($row['photo'])) {
                                                               echo "upload/noimage.jpg";
                                                           } else {
                                                               echo $row['photo'];
                                                           } ?>" height="170px" width="80%"></a></td>
                           <td class="productname1"><?php echo $row['productname']; ?></td>
                           <td class="price">Rs <?php echo number_format($row['price'], 2); ?></td>
                           <td><input type="number" class="form-control" name="quantity<?php echo $iterate; ?>"></td>
                       </tr>
                   <?php
                       $iterate++;
                   }
                   ?>
               </tbody>
           </table>

           <div class="row">
               <div class="col-md-3">
                   <input type="text" name="customer" class="form-control" placeholder="Customer Name" required>
               </div>
               <div class="col-md-3">
                   <input type="number" name="number" class="form-control" placeholder="Contact Number" required>
               </div>
               <div class="col-md-2" style="margin-left:-20px;">
                   <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Order</button>
                   <br />
                   <br />
                   <br />

               </div>
           </div>
       </form>
   </div>
   <script type="text/javascript">
       $(document).ready(function() {
           $("#checkAll").click(function() {
               $('input:checkbox').not(this).prop('checked', this.checked);
           });

           $('#order-form').submit(function(e){
               let $quantities = $(this).find('.table input[type="number"]').filter(function(){
                   return $(this).closest('tr').find('input[type="checkbox"]').is(':checked') && $(this).val() === '';
               })
               if( $quantities.length > 0 ){
                   e.preventDefault();
                   alert("Quantity is required")
                   $quantities.first().focus()
               }
           })
       });
   </script>
</body>

In case of users who have disabled Javascript, you should also check on the server for missing quantities:

// purchase.php
if (isset($_POST['productid'])) {
  for ( $i=0; $i < sizeof($_POST['productid']); $i++ ) {
    if ( $_POST['productid'][$i] && ! $_POST['quantity'][$i] ) {
      echo "Missing quantity for " . $_POST['productid'][$i] . "<br>";
    }
  }
}
Related