|
|
|
พี่ค่ะช่วยดูโค๊ดให้หน่อยค่ะ เรื่องการคำนวน คือจำนวนเงินทั้งหมดจะเปลี่ยนไปตามจำนวนเงินที่ได้กรอกลงไป |
|
|
|
|
|
|
|
Code (PHP)
<?php
session_start();
if($_SESSION['Mem_ID'] == "")
{
echo "Please Login!";
echo sss.$_SESSION['Mem_ID'];
exit();
}
$objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
$objDB = mysql_select_db("household");
mysql_query("SET character_set_results=utf8");
mysql_query("SET character_set_client=utf8");
mysql_query("SET character_set_connection=utf8");
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript">
function num_key(evt){
var iKeyCode;
if(window.event){ // IE
iKeyCode = evt.keyCode
}
else if(evt.which){ // Netscape/Firefox/Opera
iKeyCode = evt.which
}
if ((iKeyCode >= 48 && iKeyCode <= 57) || iKeyCode == 8 || iKeyCode == 46){ // 8 Backapace , 46 Delete , .
return true;
}
else{
return false;
}
}
function calculate(_id,_type){
var _hdTotal = document.getElementById("hdtotal").value;
var _hdNum = document.getElementById("hdNum").value;
var _result;
if (_type == "pc") { // เปอร์เซนต์
var _percent = document.getElementById("percent"+_id).value;
if(isNaN(_percent)) _percent = 0;
if (_percent != 0) {
_result = ((_hdTotal / 100)*_percent);
document.getElementById("amt"+_id).value = _result.toFixed(2);
}
else{
document.getElementById("amt"+_id).value = "";
}
//Check %
var _sum=0;
var _sumVal=0;
for(var i=1;i<_hdNum;i++){
var _pc = document.getElementById("percent"+i).value;
if(isNaN(_pc)) _pc = 0;
_sum += (_pc * 1);
if(_sum > 100 && _pc != 0){ // > 100%
alert("เกิน 100 % ค่ะ")
for(var j=1;j<i;j++){
var _val = document.getElementById("percent"+j).value;
if(isNaN(_val)) _val = 0;
_sumVal += (_val * 1);
}
_percent = (100 - _sumVal); // %
_result = ((_hdTotal / 100) * _percent); // amt
document.getElementById("percent"+i).value = _percent;
document.getElementById("amt"+i).value = _result.toFixed(2);
}
}
}
else if (_type == "amt") { // จำนวนเงิน
var _amt = document.getElementById("amt"+_id).value;
if(isNaN(_amt)) _amt = 0;
if (_amt != 0) {
_result = (100-((_hdTotal - _amt)/_hdTotal*100));
document.getElementById("percent"+_id).value = _result.toFixed(2);
}
else{
document.getElementById("percent"+_id).value = "";
}
//Check %
var _sum=0;
var _sumVal=0;
for(var i=1;i<_hdNum;i++){
var _pc = document.getElementById("percent"+i).value;
if(isNaN(_pc)) _pc = 0;
_sum += (_pc * 1);
if(_sum > 100 && _pc != 0){ // > 100%
alert("เกิน 100 % ค่ะ")
for(var j=1;j<i;j++){
var _val = document.getElementById("percent"+j).value;
if(isNaN(_val)) _val = 0;
_sumVal += (_val * 1);
}
if(_sumVal > 100){
_percent=0;
_result=0;
}else{
_percent = (100 - _sumVal); // %
_result = ((_hdTotal / 100) * _percent); // amt
}
document.getElementById("percent"+i).value = _percent;
document.getElementById("amt"+i).value = _result.toFixed(2);
}
}
}
var _sum=0;
for(var i=1;i<_hdNum;i++){
var _percent = document.getElementById("amt"+i).value;
if(isNaN(_percent)) _percent = 0;
_sum += (_percent * 1);
}
document.getElementById("total").value = (_hdTotal - _sum);
}
</script>
<body>
<form name="form1" method="post" action="save9.php">
<table width="572" border="1" align="center">
<tr>
<td width="50" align="center">ลำดับที่</td>
<td width="170" align="center">รายการ</td>
<td width="144" align="center">เปอร์เซ็นต์</td>
<td width="158" align="center">จำนวนเงิน</td>
</tr>
<?
$b=1;
?>
<input type="hidden" name="one" id="one" value="100"/>
<p align="center">จำนวนเงินทั้งหมด
<input type="text" name="total" id="total" value="1000">
<input type="hidden" name="hdtotal" id="hdtotal" value="1000">
<?
$table="select* from saving_type where Mem_ID='".$_SESSION['Mem_ID']."'";
//$table="select * from saving_type ORDER BY Sav_Type_ID ASC";
$table1=mysql_query($table);
$rows=mysql_num_rows($table1);
while($table3=mysql_fetch_assoc($table1)){
?>
<tr>
<td align="center"><? echo $b; ?></td>
<td align="center"><? echo $table3['sav_Type_Name']?>
<input type="hidden" name="sav_Type_ID<?=$b?>" value="<?=$table3['sav_Type_ID'];?>"></td>
<input type="hidden" name="Sav_Date_Start<?=$b?>" value="<? echo date('Y-m-d')?>">
<?php $mount = date('m');
if($mount=12){
$day=date('d'); $mount=1; $year=date('Y')+1;
}
else{
$day=date('d'); $mount=date('m')+1; $year=date('Y');
}
?>
<input type="hidden" name="Sav_Date_stop<?=$b?>" value="<? echo $year."-0".$mount."-".$day?>">
<td align="center"><input type="text" name="percent<?=$b;?>" id="percent<?=$b;?>" value="0" maxlength="3" onKeyPress="return num_key(event);" onKeyUp="calculate('<?=$b;?>','pc');"/></td>
<td align="right"><input type="text" name="amt<?=$b;?>" id="amt<?=$b;?>" value="0.0" maxlength="10" onKeyPress="return num_key(event);" onKeyUp="calculate('<?=$b;?>','amt');"/></td>
<?
$b++;
}
?>
<input type="hidden" name="hdNum" id="hdNum" value="<?=$b;?>">
</tr>
</table>
</form>
</body>
</html>
|
|
|
|
|
Date :
2013-06-01 14:47:22 |
By :
thep |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณพี่ thep มากมายค่ะ หนูทำตั้งหลายแล้ว ขอบคุณมากๆๆค่ะ
|
|
|
|
|
Date :
2013-06-01 21:22:54 |
By :
becauselove |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|