Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
<table class="table table-striped table-sm" data-maxrow="10" data-minrow="1">
<thead>
   <tr>
    <th class="bg-light"><label for="item_testname_id"><?php print_lang('item_testname_id'); ?></label></th>
    <th class="bg-light"><label for="unit_price"><?php print_lang('unit_price'); ?></label></th>
          <th class="bg-light"><label for="quantity"><?php print_lang('quantity'); ?></label></th>
          <th class="bg-light"><label for="total_price"><?php print_lang('total_price'); ?></label></th>
          <th></th>
      </tr>
  </thead>
  <tbody>
			//here is using forloop function create dynamic table row
      <?php 
      for( $row = 1; $row <= 1; $row++ ){

      ?>
    <tr class="input-row">
     <td>
      <div id="ctrl-item_testname_id-row<?php echo $row; ?>-holder" class="">
          <select required=""  id="ctrl-item_testname_id-row<?php echo $row; ?>" data-load-select-options="unit_price" name="row<?php echo $row ?>[item_testname_id]"  placeholder="<?php print_lang('select_a_value_'); ?>"    class="custom-select" >
              <option value=""><?php print_lang('select_a_value_'); ?></option>
              <?php 
              $item_testname_id_options = $comp_model -> pat_direct_billing_list_item_testname_id_option_list();
              if(!empty($item_testname_id_options)){
              foreach($item_testname_id_options as $option){
              $value = (!empty($option['value']) ? $option['value'] : null);
              $label = (!empty($option['label']) ? $option['label'] : $value);
              $selected = $this->set_field_selected('item_testname_id',$value, "");
              ?>
              <option <?php echo $selected; ?> value="<?php echo $value; ?>">
                  <?php echo $label; ?>
              </option>
              <?php
              }
              }
              ?>
          </select>
      </div>
       </td>
       <td>
        <div id="ctrl-unit_price-row<?php echo $row; ?>-holder" class="">
            <select required=""  id="ctrl-unit_price-row<?php echo $row; ?>" data-load-path="<?php print_link('api/json/pat_direct_billing_list_unit_price_option_list') ?>" name="row<?php echo $row ?>[unit_price]"  placeholder="<?php print_lang('select_a_value_'); ?>"    class="custom-select" >
                <option value=""><?php print_lang('select_a_value_'); ?></option>
            </select>
        </div>
       </td>
       <td>
		<div id="ctrl-quantity-row<?php echo $row; ?>-holder" class="">
           <input id="ctrl-quantity-row<?php echo $row; ?>"  value="<?php  echo $this->set_field_value('quantity',"1", $row); ?>" type="text" placeholder="<?php print_lang('enter_quantity'); ?>"  required="" name="row<?php echo $row ?>[quantity]"  class="form-control " />
        </div>
       </td>
       <td>
           <div id="ctrl-total_price-row<?php echo $row; ?>-holder" class="">
               <input id="ctrl-total_price-row<?php echo $row; ?>"  value="<?php  echo $this->set_field_value('total_price',"", $row); ?>" type="text" placeholder="<?php print_lang('enter_total_price'); ?>"  required="" name="row<?php echo $row ?>[total_price]"  class="form-control " />
               </div>
           </td>
           <th class="text-center">
//here is delete button
               <button type="button" class="close btn-remove-table-row"</button>
           </th>
      </tr>
           <?php 
           }
           ?>//end forloop 
          </tbody>
 <tfoot>
     <tr>
       <th colspan="100" class="text-right">
           <?php $template_id = "table-row-" . random_str(); ?>
           <button type="button" data-template="#<?php echo $template_id ?>" class="btn btn-sm btn-light btn-add-table-row">class="fa fa-plus"></button>
       </th>
     </tr>
 </tfoot>
</table>


What I have tried:

total=0;
for($row = 1; $row <= 1; $row++){
  total_price = document.getElementById('ctrl-total_price-row'+$row).value;
 quantity = document.getElementById('ctrl-quantity-row'+$row).value;
  
  r =(total_price*quantity);
  total+=r;
  document.getElementById('ctrl-total_price-row'+$row).innerHTML = r +;
}
Posted
Updated 1-Dec-22 21:36pm
v2

1 solution

PHP runs on the server. It generates a complete HTML document, which is then sent back to the user.

document.getElementById is Javascript. That runs on the client, after the PHP code has finished and sent the complete HTML document back to the server.

You cannot call a client-side function from script running on the server. You either need to use the data which is available on the server to calculate your total, or move the calculation code to the client as Javascript.

NB: Your "loop" seems off - for($row = 1; $row <= 1; $row++) will never loop; it will only execute once.
 
Share this answer
 
Comments
Member 13751016 2-Dec-22 3:29am    
//here is delete button

//here is the script for unit_price * quantity=total_price // this code work fine
function CalculateTotal(ele) {
var unit_price = $(ele).closest('tr').find('.unit_price').val();
var quantity = $(ele).closest('tr').find('.quantity').val();
unit_price = unit_price == '' ? 0 : unit_price;
quantity = quantity == '' ? 0 : quantity;

if (!isNaN(unit_price) && !isNaN(quantity)) {
var total_price = parseFloat(unit_price) * parseFloat(quantity);
$(ele).closest('tr').find('.total_price').val(total_price.toFixed(2));
}
CalculateGrandTotal();
}
//here is the script for total_price display in sub_total at footer //this code is also work fine
function CalculateGrandTotal() {
total = 0;
$(".total_price").each(function () {
total += parseInt($(this).val());
});
$("#sub_total").val(total.toFixed(2));
}
my question is when delete remove table subtract(-) from sub total
Richard Deeming 2-Dec-22 4:04am    
When you delete a row, you'll need to call CalculateGrandTotal again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900