需要帮助的这个PHP复选框是值计算脚本


Need Help For This PHP Checkbox is Value Calculation Srcipts

Assalamu'alaikum,我使用这个脚本来计算名为"Biaya"的复选框的值,它是有效的。


<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
</style>
</head>
<body>
<?php
    mysql_connect("localhost", "root")or die("cannot connect");    
    mysql_select_db("spp")or die("cannot select DB");
    $sql="SELECT `idtagihan`, `namatagihan`,`biaya`  from tagihan";
    $result=mysql_query($sql);
    $count=mysql_num_rows($result);
?>
<table border=1>
    <tr>
        <td>
            <form name="form1" method="post">
                <table>
                    <tr>
               <td>Id</td> 
                        <td>Nama</td>                      
                        <td>Harga</td>  
                    </tr>
<?php
    while($rows=mysql_fetch_array($result)){
?>
                    <tr>
                        <td><?php echo $rows['idtagihan']; ?>></td>
                        <td><?php echo $rows['namatagihan']; ?></td>
                  <td>Rp. <?php echo $rows['biaya']; ?>,-</td>
                  <td><input type="checkbox" name=check[] value="<?php echo $rows['namatagihan']; ?>" data-weight="<?php echo $rows['biaya']; ?>"></td>
                    </tr>                                   
<?php
    }
?>
                    <tr>
                        <td colspan=3><input name="Next" type="submit" id="Next" value="Next"></td>
                    </tr>

                    <?php

mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>

  <div>Total: <span id="total">0</span></div>
</body>
</html>
<script type="text/javascript">
        (function () {
            var totalEl = document.getElementById('total'),
                total = 0,
                checkboxes = document.form1['check[]'],
                handleClick = function () {
                    total += parseInt(this.getAttribute('data-weight'), 10) * (this.checked ? 1 : -1);
                    totalEl.innerHTML = total;
                },
                i, l
            ;
            for (i = 0, l = checkboxes.length; i < l; ++i) {
                checkboxes[i].onclick = handleClick;
            }
        }());
</script>

结果如下:

第一个结果pic

然后我开发的代码更新它的值到数据库,但它不能计算了。下面是整个php代码:


<?php 
// Start session 
session_start(); 
require_once('includes/functions.inc.php'); 
// Check login status... if not logged in, redirect to login screen 
if (check_login_status() == false) { 
              redirect('login.php'); 
} 
// Connection to the database
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="spp"; // Database name
$tbl_name="tagihan"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
if(isset($_POST['check'])){$checkbox = $_POST['check'];
if(isset($_POST['activate'])?$activate = $_POST["activate"]:$deactivate = $_POST["deactivate"])
$id = "('" . implode( "','", $checkbox ) . "');" ;
$sql2="UPDATE tagihan SET status = '".(isset($activate)?'Lunas':'Belum Lunas')."' WHERE idtagihan IN $id" ;
$result = mysql_query($sql2) or die(mysql_error());
}

  $nim  = $_SESSION['nim'];
$sql="SELECT `idtagihan`, `namatagihan`, `biaya` FROM tagihan WHERE nim='".$nim."' and status='Belum lunas' ";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
              <meta http-equiv="Content-type" content="text/html;charset=utf-8" /> 
              <title>Pembayaran SPP</title> 
              <link rel="stylesheet" type="text/css" href="css/gaya.css" />
              <link href="css/menu.css" rel="stylesheet" type="text/css">
</head> 
<body> 
<div class="halaman">
              <h1>Pembayaran SPP</h1> 
              <p><a href="includes/logout.inc.php">Keluar</a>
              <?php
           $nim  = $_SESSION['nim'];
           ?>
             <label>Nim anda : </label> <input type="total" name="nim" id="nim" readOnly="readonly" value="<?php echo $nim; ?>" >
              </p> 
              </br>
            <div class="tabel" >
            <form name="frmactive" method="post" action="">
                <table>
                    <tr>
               <td>Id</td> 
                    <td>Nama</td>                      
                    <td>Harga</td> 
                    <td>Pilih</td>  
                    </tr>
<?php
    while($rows=mysql_fetch_array($result)){
?>
                    <tr>
                        <td><?php echo $rows['idtagihan']; ?></td>
                        <td><?php echo $rows['namatagihan']; ?></td>
                  <td>Rp. <?php echo $rows['biaya']; ?>,-</td>
                  <td><input class="css-checkbox" type="checkbox" name='check[]' name='cek[]' value="<?php echo $rows['idtagihan']; ?>" biaya = "<?php echo $rows['biaya']; ?>"></td>
                    </tr>                                         
<?php
    }
?>                 </table> 
<div class="tabelspp" >
<table> 
            <tr>
<td><label>Total Rp.</label><input type="total" id="total2" readOnly="readonly"></input><label>,-</label>
    <input type="submit" name="activate" id="activate" value="Bayar" /> </td>
</tr> 
</table> </form> </div>
  </div>
</div>               
</div>
            </div>
            </div>
</body> 
</html> 
<script type="text/javascript">

        (function () {
            var totalEl = document.getElementById('total2'),
                total2 = 0,
                checkboxes = document.form1['check[]'],
                handleClick = function () {
                    total2 += parseInt(this.getAttribute('biaya'), 10) * (this.checked ? 1 : -1);
                    totalEl.value = total2;
                },
                i, l
            ;
            for (i = 0, l = checkboxes.length; i < l; ++i) {
                checkboxes[i].onclick = handleClick;
            }
        }());
</script>

结果如下:

第二个结果pic

我可以成功地更新数据库。但我的问题是如何使第二个脚本可以计算值的复选框再次相同的第一个脚本?我需要解决这个问题,我希望有人能回答这个问题。如果有人回复我的帖子,我很高兴。谢谢你。

首先,尝试更改表单的名称从

<form name="frmactive" method="post" action="">

<form method="post" action="" name="form1">