1

The title might not seem clear but this is my problem.

A user can check either 1 or 2 checkboxes. When the checkbox is checked, it asks for the quantity. enter image description here

What if the user clicks both checkboxes, how can I get the total price?

The checkbox values are saved in multiple rows in MySql. The number 57 in DocumentRequest_idDocumentRequest is a foreign key from another table which means it came from the same user. Also, I want the price column for both rows to yield the same value which is 40. The value of 40 is from 30 + 10 which is evident in the column isPartOfTotal. But is shows 10 and 40, respectively. Something is wrong with my calculation/code.

enter image description here

This is my php code:

<?php
include 'config.php';

if (isset($_POST['documentRequest1']))
    {
        $chkbox = $_POST['docs'];
        $id = $_POST['a'];
        $totalPrice = 0;

        foreach($chkbox as $chk1)  
            { 
                if($chk1=='Certificate of Residency') 
                    {
                        $chek_val=$_POST['d1'];

                    }
                else if($chk1=='Barangay Clearance') 
                    {
                        $chek_val=$_POST['d2'];
                    } 

                $result = mysqli_query($conn, "SELECT price FROM document WHERE typeOfDoc = '$chk1';");
                $row = mysqli_fetch_assoc($result);
                $isPartOfTotal = $chek_val * $row["price"];
                $totalPrice = $totalPrice + $isPartOfTotal; //corresponds to the price column in the table.

                $sql = mysqli_query($conn, "INSERT into requestitem (DocumentRequest_idDocumentRequest, Document_idDocument, quantity, isPartOfTotal, price) VALUES ((SELECT idDocumentRequest FROM documentrequest WHERE Person_idPerson = '$id'), (SELECT idDocument FROM document WHERE typeOfDoc = '$chk1'), '$chek_val', '$isPartOfTotal', '$totalPrice');");
            } 

        echo "You have succesfully submitted a new record.";
        mysqli_close($conn);    
    }                               
?>

I hope I explained it clearly. Please help me. Thank you so much.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Isabella
  • 455
  • 1
  • 10
  • 23
  • 1
    You're doing the inserts inside the loop, but you don't get the total price until the loop is done. You need to do the inserts after the loop that calculates the total. – Barmar Dec 31 '15 at 22:13
  • If you're adding a column for each input, why wouldn't you want them both to include their own prices? How would having them both store the total make any sense? An I missing something? – erik258 Dec 31 '15 at 22:14
  • Hmm.. outside the loop. I'll try this one. Thanks. [@Barmar] – Isabella Dec 31 '15 at 22:17
  • Actually, I could just eliminate the total but it was in our instructions. [@Dan Farrell] – Isabella Dec 31 '15 at 22:18

0 Answers0