douxun2023 2013-06-20 10:04
浏览 56
已采纳

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

Assalamu'alaikum, i am use this scripts for calculate the value of checkbox named "Biaya" and it is works.


<!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>

The result is like this :

First result pic

Then i develop that code for updating it's value to database but it cannot calculate anymore. Here is the whole php code :


<?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>

Then here is the result :

Second result pic

I can update the database successfully. But my question is how to make that second script can calculate the value's checkbox again same as first script? I need to resolve this, i hope there someone who can answer this. I am happy if there anyone reply my post. Thank you.

  • 写回答

1条回答 默认 最新

  • dougao9864 2013-06-20 11:19
    关注

    for first, try to change the name of the form from

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

    to this

    <form method="post" action="" name="form1">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作