Welcome Guest, Not a member yet? Register   Sign In
Sum value checkbox js
#1

(This post was last modified: 11-30-2014, 09:10 AM by bobykurniawan.)

Hey goodmorning, maybe my question won't specified with 'Codeigniter' but i really need a help here, i get stuck.
PHP Code:
<script type="text/javascript">
 
   function checkTotal() {
 
  document.listForm.total.value '';
 
   var sum 0;
 
   for (i=0;i<document.listForm.pilihan.length;i++) {
 
     if (document.listForm.pilihan[i].checked) {
 
        sum sum parseInt(document.listForm.pilihan[i].value);
 
     }
 
   }
 
   document.listForm.total.value sum;
 
 }
</
script>
<
div class="container">
<
form  name ="listForm" class="form-horizontal" method="POST" action="<?=base_url();?>transaksi/tesjs">
 
 <div class="checkbox">
 
  <?php foreach ($listdonasi as $key) { ?> 
       <label> 
    <input type="checkbox" class="chk" name="pilihan" value="<?=$key->minimaldonasi;?>" onchange="checkTotal()" /><?=$key->namadonasi;?> (Biaya : Rp. <?=$key->minimaldonasi;?>)
       </label>
       <br>
    <?php  ?>
  </div>
<div class="form-group">
<div class="col-md-4">

Total: <input type="text" name="total" value="0"/>
    </div>
</div>
</div>
</form>
</div> 

As you can see from my script above, when i check my checkbox, it'll  sum the value. But i want to add "id" that i take from table then save it into other table. 

So i create this script, but my js not working. 
PHP Code:
<div class="checkbox">
 
 <?php foreach ($listdonasi as $key) { ?> 
      <label> 
   <input type="checkbox" class="chk" name="pilihan[]" value="<?=$key->iddonasi;?>" onchange="checkTotal()" /><?=$key->namadonasi;?> (Biaya : Rp. <?=$key->minimaldonasi;?>)
      </label>
      <br>
   <?php  ?>
 </div>
<div class="form-group">
<div class="col-md-4">

Total: <input type="text" name="total" value="0"/>
   </div>
</div> 

Is there any other method ?
Reply
#2

I don't understand the question.... I lose it at "But i want to add "id" that i take from table then save it into other table." I am not sure what that means.

In your second code block, you post php code but say your javascript is not working. If the javascript is not working, post the actual output of that page from the document source, not the php you are using to create the page.
Reply
#3

(This post was last modified: 11-30-2014, 10:04 AM by bobykurniawan.)

(11-30-2014, 09:47 AM)bclinton Wrote: I don't understand the question....  I lose it at "But i want to add "id" that i take from table then save it into other table."  I am not sure what that means.

In your second code block, you post php code but say your javascript is not working.  If the javascript is not working, post the actual output of that page from the document source, not the php you are using to create the page.

I'm sorry, i'm not good in english. 

When you want to insert into table you will use the value from the checkbox right? 

now, from my first code i use my checkbox value for sum 'price' from my table and my second code i don't sum the 'price' because i want to save 'id (iddonasi from my table)'. How to join it together ?

Ahh, if i good in english. I'll explain it clearly Sad
Reply




Theme © iAndrew 2016 - Forum software by © MyBB