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>
结果如下:
然后我开发代码来更新它的值到数据库,但它不能再计算。这是整个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>
然后是结果:
我可以成功更新数据库。但我的问题是如何使第二个脚本可以再次计算值的复选框与第一个脚本相同?我需要解决这个问题,我希望有人能够回答这个问题。如果有人回复我的帖子,我很高兴。谢谢。
答案 0 :(得分:0)
首先,尝试更改表单的名称 从
<form name="frmactive" method="post" action="">
到这个
<form method="post" action="" name="form1">