Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

Need Help For This PHP srcipts

General discussions related to php

Moderators: macek, egami, gesf

Need Help For This PHP srcipts

Postby AnggaLisdiyanto » Thu Jun 20, 2013 1:36 am

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

Code: Select all
<!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 :

1.PNG
1.PNG (4.6 KiB) Viewed 468 times


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

Code: Select all
<?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 :
2.PNG
2.PNG (19.03 KiB) Viewed 468 times

I need to resolve this, i hope there someone who can answer this. I am happy if there anyone reply my post. Thank you.
AnggaLisdiyanto
New php-forum User
New php-forum User
 
Posts: 1
Joined: Thu Jun 20, 2013 1:21 am

Re: Need Help For This PHP srcipts

Postby johnj » Thu Jun 20, 2013 9:57 pm

In your javascript, display(alert) the values of totalEl and total and see what is going wrong there.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm


Return to PHP General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.