Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with trying to validate form data from my html file using javascript.

ID: 3843058 • Letter: N

Question

Need help with trying to validate form data from my html file using javascript.

In a javascript file:

I want to validate that when a checkbox has been checked then a value needs to be entered in the number box corresponding to the checkbox. For example, if the user selects "Cereal" with the checkbox option, I want the javascript program to display an alert as "Please enter a quantity for the selected item" ONLY if they have selected the item but they have not entered a quantity.

If the user does not select an item to begin with then the javascript function should not apply.

Also, an alert should pop up if the user tries to enter a quantity value more than 5. Ex. "You cannot enter a value greater than 5."

I also want to validate if the user selects a payment option via radio buttons. For example, if the user does not select a payment option then an alert will pop up prompting the user to select a payment option.

Here is what I have so far on my HTML file:


  Grocery Item Selection

Grocery Item Selection




  
  

Grains


  

  
  

  


      
      
      
      
      
  

  


      
      
      
      
  
  
  
      
      
      
      
  
  
  
      
      
      
      
  






  

Payment Method


  
  

Select payment method:


   Visa

   Master Card

   Discover

  


  
      


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <title>Delivery Options</title>
</head>

<body>

   <?php

       $username = $_SESSION["username"];
       //variables being passed in from previous page
       $item_name = $_POST['item_name'];
       $item_quantity = $_POST['item_quantity'];
       //$item_quantity = array_filter($item_quantity);
       $payment_method = $_POST['payment'];
       // price of items selected from DB
       $item_cost = array();
       // derived variables
       $arraySize = sizeof($item_quantity);
       $quantity_parsed = array();

       print("$username");


       // remove null values from item_quantity
       foreach ($item_quantity as $temp) {
           if($temp != null){
               array_push($quantity_parsed, "$temp");
           }
       }

   $db = mysqli_connect(xxxx,xxxx,xxxx,xxxx);

   if (mysqli_connect_errno())
       exit("Error - could not connect to MySQL");

   //loop for storing selected item prices
   foreach($item_name as $temp){
           //find current item price
           $price_query = "SELECT price FROM item
               where item_name = '$temp'";

           //push price onto array
           $temp_price = mysqli_query($db, $price_query);
           $row_array = mysqli_fetch_array($temp_price);
           array_push($item_cost, "$row_array[price]");

   }

       ?>
<h1 class="orders">Order Deliver Options</h1>
<form method="post" action="order_invoice.php">
<div class ="purchase">

   <table class="delivery_form">

   <!-- headers -->
   <tr>
       <th>Items</th>
       <th>Price</th>
       <th>Quantity</th>
       <th>Dilivery Options</th>
   </tr>

<?php

   $length = sizeof($item_name);

   for ($i = 0; $i < $length; $i++) {
   ?>
           <tr>
           <td><?php print("$item_name[$i]"); ?></td> <td><?php print("$item_cost[$i]");?></td> <td><?php print("$quantity_parsed[$i]");?></td>
           <td>
           <select name = "delivery_option[]">
           <option value = "2-day delivery">2-day delivery</option>
           <option value = "bi-weekly">bi-weekly</option>
           <option value = "weekly">weekly</option>
           <option value = "monthly">monthly</option>
           </select>
           </td>
       </tr>

</table>

   <input type = "submit" value="Place Order" />
   <input type = "reset" value="Reset form" />
</div>

</body>
</html>

Select item Item Price Quantity Cereal $3.00
           Rice $4.00
           Pasta $5.00
          

Explanation / Answer

Include the following Script to your program.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote