2

I have no idea how to calculate total price of this subjects using php. help me with it.

<div id="mydiv" style="display:none">   
   <input type="checkbox" name="subject[]" value="biology">biology<br>
   <input type="checkbox" name="subject[]" value="physics">physics<br>
   <input type="submit" value="Calculate" name="submit" class="wpcf7-submit">
</div>

<div id="mydiv1" style="display:none">
   <input type="checkbox" name="subject[]" value="maths">maths<br>
   <input type="checkbox" name="subject[]" value="science">science<br>
   <input type="submit" value="Calculate" name="submit" class="wpcf7-submit">
</div>

I want to calculate price total of this checkboxes and store it in database with its names using php

php part

if(isset($_POST['subject']))
    {
        $classes=$_POST['subject'];
            $prices = array(
        'biology' => 60,
        'physics' => 200

    );
            $sum = array();
    $getkeys = array_keys($_POST);
    foreach($prices as $key => $value)
    {
        if(in_array($key, $getkeys)) $sum[] = $value;
    }
    $ar=array_sum($sum);
    echo $ar;

        if($classes)
        {

        $subject = implode(',', $ar);

        $query="INSERT INTO feedetails (subjects,price) VALUES ('".$subject."','".$price."')";

        if(mysqli_query($conn,$query))
        {
            echo ("<SCRIPT LANGUAGE='Javascript'>
        window.alert('Your fee has been updated.Please proceed to pay.');
        window.location.href='payment.php';
        </SCRIPT>");
        }
        }   
ajmedway
  • 1,492
  • 14
  • 28
Ashwini Nemade
  • 153
  • 1
  • 13

2 Answers2

1

It appears that your summing strategy was not working, there were numerous issues there including this:

$getkeys = array_keys($_POST);

which appears as an attempt to get the subjects submitted, however they are in their own sub-array of $_POST, i.e. $_POST['subject']

This gets you the summing of the price information you require, however you will need to test your database INSERT code and debug this to ensure you are storing the data required correctly.

if (!empty($_POST['subject'])) {
    $prices = [
        'biology' => 60,
        'physics' => 200,
        'maths' => 300,
        'science' => 400,
    ];
    $sum = 0;

    foreach ($_POST['subject'] as $subject) {
        if (!empty($prices[$subject])) {
            $sum += $prices[$subject];
        }
    }
    echo '<pre>';
    echo '$sum ' . print_r($sum, true);
    echo '</pre>';
    exit;

    // process database inserts here
}

Additionally, when testing your code I notice you had hidden the checkboxes, to resolve this, use the following:

<div id="mydiv">   
    <input type="checkbox" name="subject[]" value="biology">biology<br>
    <input type="checkbox" name="subject[]" value="physics">physics<br>
    <input type="checkbox" name="subject[]" value="maths">maths<br>
    <input type="checkbox" name="subject[]" value="science">science<br>
    <input type="submit" value="Calculate" name="submit" class="wpcf7-submit">
</div>
ajmedway
  • 1,492
  • 14
  • 28
0

Try This may be help:

I am not doing all code just you asked for sum part.

Rest you can do Ask if any confusion.

HTML

<form method='post'>        

    <div id="mydiv" >   
    <input type="checkbox" name="subject[]" value="biology">biology<br>
     <input type="checkbox" name="subject[]" value="physics">physics<br>
    <input type="checkbox" name="subject[]" value="maths">maths<br>
   <input type="checkbox" name="subject[]" value="social">social<br>
    <input type="checkbox" name="subject[]" value="ssc">ssc<br>


    <input type="submit" value="Calculate" name="submit" 
    class="wpcf7- 
   submit">
   </div>

PHP

   <?php
      $classes =$_POST['subject'];

        $prices = array('biology' => 60,'physics' => 
              200,'maths'=>100,'ssc'=>40,'social'=>150);
        $sum = 0;

        echo 'Subject::Price<hr/>';
        foreach($classes as $key=>$sub){

           if(array_key_exists($sub,$prices)){
            echo $sub.'::'.$prices[$sub]."<br />";
            $sum = $sum+$prices[$sub]; 
           }
      }
       echo '<hr/>';
      echo $sum;
     ?>

OUTPUT

enter image description here

Er. Amit Joshi
  • 611
  • 5
  • 21