jeudi 31 décembre 2015

How to get total price if the user has checked multiple checkboxes?

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.




Aucun commentaire:

Enregistrer un commentaire