0

This is my code

<html>
<head>
    <title>
        Aplikasi KRS
    </title>
</head>
<body>
<p>IPK : 1.xx</p>
    <form name="formTes" method="post">
        <script>
            function cekKrs()
            {
                var jum_sks = 0;
                for(var i=0; i<7; i++)
                {
                    if(document.formTes.elements[i].checked)
                        jum_sks += mataKuliah;
                }


                document.formTes.fieldJumlah.value = jum_sks;

                if(jum_sks > 15)
                {
                    alert("Anda harus mengurangi SKS\n karena melebihi 15 SKS");
                }

            }

            function cekKirim()
            {
                if(document.formTes.fieldJumlah.value > 15)
                    alert("Anda harus mengurangi SKS\n karena melebihi 15 SKS");
                else
                    window.location.href = "tesform.html";
            }
        </script>

        <table border="2">
        <tr><th>Kode</th><th>Mata Kuliah</th><th>SKS</th><th><Pilihan/th></tr>
        <?php 
            include "koneksi.php";
            $sql = "SELECT * FROM mata_kul";
            $query = $con->query($sql);
            while($mataKuliah = $query->fetch_array())
            {
                echo "<tr><td>". $mataKuliah['kodeMK'] ."</td>";
                echo "<td>". $mataKuliah['namaMK'] ."</td>";
                echo "<td>". $mataKuliah['sks'] ."</td>";
                echo "<script>var mataKuliah= ".$mataKuliah['sks']."</script>";
                echo "<td><input name='mk". $mataKuliah['kodeMK'] ."' type='checkbox' onclick='cekKrs()'></td>";
            }
        ?>
        <tr><td colspan="2" align="right">Jumlah SKS</td>
        <td colspan="2"><input size="3" value="0" name="fieldJumlah" type="text" readonly="readonly"></td></tr></table>
        <br>
        <input type="button" name="tombolKirim" value="Kirim" onclick="cekKirim()">
    </form>
</body>
</html>

My question is

"Why when i click checklist it show the last value of sks in jumlah SKS, not the sks checked value i click ? like when i click checklist which have value of 2 in sks it should show 2 not 8 in jumlah SKS"

Here's the imageIt's my php

Can you tell me where did i go wrong ? I'm still new

4
  • 1
    You have a lot of variables named 'mataKuliah' in javascript... the last one = 8 ... Commented Feb 11, 2015 at 3:12
  • 1
    I guess you need send the checkbox obj as param to cekKrs() , and in function cekKrs evaluates if obj is checked to + or - the obj.value from total Commented Feb 11, 2015 at 3:21
  • I already send the value <input name='mk". $mataKuliah['kodeMK'] ."' type='checkbox' onclick='cekKrs(mataKuliah)> then in function cekKrs(var mataKuliah) but now it's not showing any number at all :( Commented Feb 11, 2015 at 3:28
  • 1
    You should insert your HTML script so other can check it easily if it relate to javascript issue Commented Feb 11, 2015 at 6:04

1 Answer 1

2

I guess you need send the checkbox obj as param to cekKrs() , and in function cekKrs evaluates if obj is checked to + or - the obj.value from total

Try this DEMO

HTML ( draft )

<table>
<!-- while($mataKuliah = $query->fetch_array()) 
        { -->
    <tr>
      <!-- echo "<td><input name='mk". $mataKuliah['kodeMK'] ."' type='checkbox' onclick='cekKrs(this)' value='".$mataKuliah['sks']."'></td>";    // PHP  -->
       <td><input type='checkbox' onclick='cekKrs(this)' value='1'>1</td>
    </tr>
    <tr>
       <td><input type='checkbox' onclick='cekKrs(this)' value='2'>2</td>
    </tr>
    <tr>
       <td><input type='checkbox' onclick='cekKrs(this)' value='3'>3</td>
    </tr>
    <!-- } -->
</table>
<input size="3" value="0" name="fieldJumlah" id='fieldJumlah' type="text" readonly="readonly">

onclick='cekKrs(this)' sends checkbox obj , and you keep the mataKuliah's value inside the checkbox value

JS

 function cekKrs(chkObj)
            {
                //Get current total
                var jum_sks = parseInt(document.getElementById('fieldJumlah').value);
                //Get checkbox value clicked
                var mataKuliah = parseInt(chkObj.value);

                    //Evaluate check to + or -
                    if(!chkObj.checked)
                    {
                        mataKuliah = (mataKuliah * -1)
                    }

                    jum_sks += mataKuliah;

                 //Set new total
                 document.getElementById('fieldJumlah').value = jum_sks;


            }
Sign up to request clarification or add additional context in comments.

4 Comments

Yeah, it's working in the example, but, i'm using database and array, and looping while :( did not exactly specify id manually :( i try what you suggest on it, it did not show any chnge when click :( but thnx for the answer :) i have something to do, I'll see it later thnx for the answer. :)
Great! Check php commented code. It's the same, you only need add value='".$mataKuliah['sks']."' to your checkbox ... and change cekKrs(this)
You don't need any id manually or concatenated complex names . I added id='fieldJumlah' for a total, because I didn't use a form
Wow, it's working :) thnx :) I forgot to change name with id in field jumlah the other day :( there's no id before so it did not chnge anything :( but it worked now, thnx :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.