Azam Khan
Azam Khan

Reputation: 9

multiple checkbox and price calculation using php

i have table service in database db. it has 3 fields id, service_type, amount.

i have html form which has checkbox to select service type,i got service type selected but amount must be calculated auto and store in database amount column. but one error when i select service from checkbox it is selected and store in database but price is not stored. my script is as follow:

       <input type="checkbox" name="services[]" value="oilchange" 
      <input type="checkbox" name="services[]" value="acrepair" 
      <input type="checkbox" name="services[]" value="tyrechange" 

    <input type="submit" name="btnservice"  value="Confirm Services">

now php script is as follow:

<?php $price=0.0;if (isset($_POST['services'])) {

$service=$_POST['services'];
$c=count($service);



for ($i=0; $i < $c; $i++) { 
     if ($service[$i]==fullservice) {

        $price=$price+5000;
        $iq="INSERT INTO tblservices( amount) VALUES ('$price')";
        $run=mysqli_query($con,$iq);
        if ($run) {

        }
     }
      elseif ($service[$i]==tyrebalance) {
         # code...
        $price=$price+4000;
        $iq="INSERT INTO tblservices( amount) VALUES ('$price')";
        $run=mysqli_query($con,$iq);
        if ($run) {

        }
     }
      elseif($service[$i]==oilchange) {
         # code...
        $price=$price+3000;
        $iq="INSERT INTO tblservices( amount) VALUES ('$price')";
        $run=mysqli_query($con,$iq);
        if ($run) {

        }
     }
      elseif ($service[$i]==acrepair) {
         # code...
        $price=$price+2000;
        $iq="INSERT INTO tblservices( amount) VALUES ('$price')";
        $run=mysqli_query($con,$iq);
        if ($run) {

        }
     }
      elseif ($service[$i]==tyrechange) {
         # code...
        $price=$price+1000;
        $iq="INSERT INTO tblservices( amount) VALUES ('$price')";
        $run=mysqli_query($con,$iq);
        if ($run) {

        }
     }
}
}if (isset($_POST['btnservice'])) {
$a=$_POST['services'];
$type=implode("/",$a);
$query="insert into tblservices(service_type,amount) values('$type','$price')";
$run=mysqli_query($con,$query);
if ($run) {
             echo "<script type = \"text/javascript\">
                                alert(\"Services Selected.................\");
                                window.location = (\"services.php\")
                                </script>";
                            }
                    else{
                           echo "<script type = \"text/javascript\">
                                alert(\"Login Failed. Try Again................\");
                                window.location = (\"services.php\")
                                </script>";
                        }


        }?>

i'm new guys sorry that's why i cann't upload images i need to earn reputations so i could able to post images for better or detailed explanation of my question.

Upvotes: 0

Views: 1590

Answers (1)

Santhy K
Santhy K

Reputation: 839

i could see two type of queries in your sample. Sharing here single service types insertion and insertion with total price. Hope this helps!!

<div class="container">
    <form action="" method="post">
        <input type="checkbox" name="services[]" value="oilchange"  />oilchange
        <input type="checkbox" name="services[]" value="acrepair"  />acrepair
        <input type="checkbox" name="services[]" value="tyrechange" />tyrechange
        <br/>
        <input type="submit" name="btnservice"  value="Confirm Services">
    </form> <?php
    $serviceChargeArr = array(
        'fullservice' => 5000,
        'tyrebalance' => 4000,
        'oilchange' => 3000,
        'acrepair' => 2000,
        'tyrechange' => 1000
    );
    $priceTotal = 0.0;
    if (isset($_POST['btnservice'])) {    
        if (isset($_POST['services'])) {
            $serviceArr    = $_POST['services'];
            $serviceArrCnt = count($serviceArr);

            //QUERY 1 - insert each with respect to clicked checkboxes
            for ($i = 0; $i < $serviceArrCnt; $i++) {
                if ($eachPrice = $serviceChargeArr[$serviceArr[$i]]) {
                    $insertArr[] = "('$serviceArr[$i]', '$eachPrice')";
                }
            }
            if (!empty($insertArr)) {//sigle insert query -each service and its price will be inserted
                $query="insert into tblservices (service_type, amount)
                        values".implode(", ",$insertArr ).";";
                echo $query."<br/>";
            }//QUERY 1 ENDS

            //QUERY 2 - insert single row with comma separated values and total price
            for ($i = 0; $i < $serviceArrCnt; $i++) {
                if ($eachPrice = $serviceChargeArr[$serviceArr[$i]]) {
                    $priceTotal += $eachPrice;
                }
            }
            if (!empty($serviceArr)) {
                $seviceList = implode(',', $serviceArr);
                $query="insert into tblservices (service_type, amount)
                        values ('{$seviceList}' , '{$priceTotal}') ;";
                echo $query."<br/>";
            }//QUERY 2 ENDS

            if($query) {
                $run = mysqli_query($con, $query);
                if ($run) {
                    echo "<br/> SUCCESS : inserted";
                } else {
                    echo "<br/> ERROR : try again<br/>Mysql Error: ".$con->error;
                }
            }
        }
    }  ?>
</div>

Upvotes: 1

Related Questions