AMIT SUTAR MUMBAIKAR
AMIT SUTAR MUMBAIKAR

Reputation: 45

How to calculate amount by javascript?

As u can see i have this form that calculate number of employees multiply by test that will be select by user. number of employees will be input by user and test are coming from database. each test have their individual price set in database. I used script for dropdown list.

If user put any value in no.of employees and check on anytest then in total amount sum should be calculate onclick buytest?

<form action="add_corporate_test.php" method="POST">
    <script type="text/javascript">
        $(document).ready(function () {
            $('#example-onDeselectAll').multiselect({
                includeSelectAllOption: true,
                onDeselectAll: function () {
                    alert('please select any test!');
                }
            });
        });
    </script>
    <div class="row">
        <h5 class="cor_label">No. of Emp/Students:</h5>
        <input
            type="text" id="no_emp"
            placeholder="No. of Employees/Students"
            class="normal" required
        >
        <h5 class="cor_label">Email Address:</h5>
        <input
            type="text"
            id="no_emp2"
            placeholder="Email Address"
            class="normal" value="<?php echo $_SESSION[email]; ?>"
            required
            disabled
        >
        <h5 class="cor_label">Select Test:</h5>
        <select id="example-onDeselectAll" multiple="multiple">
            <?php
            $sql = mysql_query("select * from `test` ");
            while ($data = mysql_fetch_assoc($sql)) {
                $sum = 0;
                ?>
                <option value="<?php echo $data['test_name']; ?>">
                    <?php echo $data['test_name']; ?>
                </option>
            <?php } ?>
        </select>
        <h5 class="cor_label">Total Amount:</h5>
        <input
            type="text"
            id="no_emp3"
            class="normal"
            name="total_amount"
            placeholder="total amount"
            value="<?php echo $sum; ?>"
            disabled
        >
        <button class="btn btn-primary" type="submit" name="corporate_buy">
            Buy Test
        </button>
    </div>
</form>

Upvotes: 1

Views: 174

Answers (2)

Hi bro, use for loop for script b'coz u have multiple select option `

<script>
$('#example-onDeselectAll').change(function() {
var value = $(this).val();
var i=0; 
var len = value.length;
var no_emp = $("#no_emp").val();
  for(i; i < len; i++){
var res = value[i] .split("_");
var tot_amount += res[1]*no_emp;
}
$("#no_emp3").val(tot_amount);
});
</scrit> 

`

Upvotes: 1

get the test and value on select option. on select split the value using javascript. and then sum the amount and no.of.employee

`

<select id="example-onDeselectAll" multiple="multiple" >
            <?php   
                    $sql=mysql_query("select * from `test` ");
                while($data=mysql_fetch_assoc($sql))
                {
                    $sum=0; 
                ?>                                  
               <option  value="<?php echo $data['test_name'];?>_<?php echo $data['price'];?>"><?php echo $data['test_name'];?></option>                             
            <?php }?>                     
            </select>  

<script>
$('#example-onDeselectAll').change(function() {
var value = $(this).val();
var no_emp = $("#no_emp").val();
var res = value .split("_");
var tot_amount = res[1]*no_emp;
$("#no_emp3").val(tot_amount);
});
</scrit>    

`

Upvotes: 1

Related Questions