|
|
|
การคำนวณ textboxอัตโนมัติ หลาย textbox ไม่เกินร้อยเปอร์เซนต์ |
|
|
|
|
|
|
|
Code (PHP)
<?php
$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 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>จัดสรรรายจ่ายย้อนหลังในรอบ 3 เดือน</title>
<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 errcolor = "#FFE4E1";
var wcolor = "#ffffff";
var _hdTotal = document.getElementById("total").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;
for(var i=1;i<_hdNum;i++){
document.getElementById("percent"+i).style.background=wcolor;
document.getElementById("amt"+i).style.background=wcolor;
var _pc = document.getElementById("percent"+i).value; // %
if(isNaN(_pc)) _pc = 0;
_sum += (_pc * 1);
if(_sum > 100 && _pc != 0){ // > 100%
document.getElementById("percent"+i).style.background=errcolor;
document.getElementById("amt"+i).style.background=errcolor;
}
}
}
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;
for(var i=1;i<_hdNum;i++){
document.getElementById("percent"+i).style.background=wcolor;
document.getElementById("amt"+i).style.background=wcolor;
var _pc = document.getElementById("percent"+i).value; // %
if(isNaN(_pc)) _pc = 0;
_sum += (_pc * 1);
if(_sum > 100 && _pc != 0){ // > 100%
document.getElementById("percent"+i).style.background=errcolor;
document.getElementById("amt"+i).style.background=errcolor;
}
}
}
}
</script>
</head>
<body>
<fieldset>
<legend align="center">จัดสรรรายจ่ายย้อนหลังในรอบ 3 เดือน</legend>
<p align="center"><? //include ("sum_amount.php");?>
<p align="center">
<form action="sav_b.php" method="POST">
<table width="523" height="129" border="1" align="center">
<tr>
<td width="60" align="center" valign="middle">ลำดับที่</td>
<td width="159" align="center" valign="middle">รายการ</td>
<td width="138" align="center" valign="middle">เปอร์เซนต์<br /></td>
<td width="138" align="center" valign="middle">จำนวนเงิน</td>
</tr>
<?php
$sum_rc="select sum(Inc_Amount) as Sum_Amount from income where Mem_ID='".$_SESSION['Mem_ID']."' ";
$query_sum=mysql_query($sum_rc);
$result = mysql_fetch_array($query_sum);
$sum_rc2="select sum(Out_Amount) as Sum_Amount from outcome where Mem_ID='".$_SESSION['Mem_ID']."' ";
$query_sum2=mysql_query($sum_rc2);
$result2 = mysql_fetch_array($query_sum2);
$a=$result['Sum_Amount'];
$b=$result2['Sum_Amount'];
$c=$a-$b;
$sum_budget="select sum(Bud_Out_Gold) as Sum_Bud from budget_outcome where Mem_ID='".$_SESSION['Mem_ID']."' ";
$sum_budget_re=mysql_query($sum_budget);
$sum_budget_result = mysql_fetch_array($sum_budget_re);
$sum_bud=$sum_budget_result['Sum_Bud'];
$b=1;
?>
<p align="center">จำนวนเงินทั้งหมด <input type="text" name="total" id="total" value="<?=$sum_bud;?>">
<?php
$table="select * from saving_type ORDER BY Sav_Type_ID ASC";
$table1=mysql_query($table);
$rows=mysql_num_rows($table1);
//echo $rows;
while($table3=mysql_fetch_assoc($table1)){
?>
<tr>
<td align="center"><?=$b;?></td>
<td align="center"><?=$table3['sav_Type_Name'];?></td>
<td align="right"><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>
</tr>
<?php
$b++;
}
?>
<tr>
<td height="45"> </td>
<td> </td>
<td align="right"><label for="textfield3"></label></td>
<td align="right"><p>
<label for="textfield"></label>
<input type="hidden" name="hdNum" id="hdNum" value="<?=$b;?>">
</p>
</td>
</tr>
</table>
</form>
<p><a href="index_user.php">กลับ</a></p>
</fieldset>
</body>
</html>
|
|
|
|
|
Date :
2013-05-17 10:27:23 |
By :
thep |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta charset="utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
function percent_chk(status){
var sum=0;
if(status==1){
$('.percent').each(function(){
sum=sum+parseInt($(this).val());
});
}else{
$('.amt').each(function(){
sum=sum+parseFloat($(this).val());
});
}
return sum;
}
function cal(a,status){
var total=$("#total").val();
var a1=0;
if(status==1)
a1=total*a/100;
else
a1=a/total*100;
return a1;
}
$('.percent').change(function(){
var v=$(this).val();
if(percent_chk(1)>100){
$(this).parent().next().find("input[type='text']").val(0);
$(this).val(0).focus();
alert("เกิน 100%");
return false;
}else
$(this).parent().next().find("input[type='text']").val(cal(v,1));
});
$('.amt').change(function(){
var v=$(this).val();
var total=$("#total").val();
if(percent_chk(2)>total){
$(this).parent().prev().find("input[type='text']").val(0);
$(this).val(0).focus();
alert("เกิน 100%");
return false;
}else
$(this).parent().prev().find("input[type='text']").val(cal(v,2));
});
$('#total').change(function(){
$('.percent').each(function(){
var a=$(this).val();
$(this).parent().next().find("input[type='text']").val(cal(a,1));
});
});
});
</script>
</head>
<body>
<?
$total=rand(3000,4000);
?>
จำนวนเงินทั้งหมด<input type="text" id="total" value="<?=$total?>" />
<table>
<tr>
<td width="40">No</td>
<td width="160">Items</td>
<td width="100">Percentage</td>
<td>Amount</td>
</tr>
<?
$arr=array(1=>"การศึกษา",1=>"ลงทุน",1=>"ค่าัรักษาพยาบาล");
echo $arr[1];
for($i=1;$i<=3;$i++){
$percent=rand(20,30);
$val=$total*$percent/100;
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>".$arr[$i]."</td>";
echo "<td><input type='text' class='percent' value='".$percent."'/></td>";
echo "<td><input type='text' class='amt' value='".$val."'/></td>";
echo "</tr>";
}
?>
<table>
</body>
</html>
ลองประยุกต์ใช้ครับ
|
|
|
|
|
Date :
2013-05-17 17:54:51 |
By :
fossil31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
<?
$arr=array(1=>"การศึกษา",2=>"ลงทุน",3=>"ค่าัรักษาพยาบาล");
echo $arr[1];
for($i=1;$i<=3;$i++){
$percent=rand(20,30);
$val=$total*$percent/100;
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>".$arr[$i]."</td>";
echo "<td><input type='text' class='percent' value='".$percent."'/></td>";
echo "<td><input type='text' class='amt' value='".$val."'/></td>";
echo "</tr>";
}
?>
ไปเชื่อม DB เองครับ
|
|
|
|
|
Date :
2013-05-17 17:58:22 |
By :
fossil31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
<?php
$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 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>จัดสรรรายจ่ายย้อนหลังในรอบ 3 เดือน</title>
<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 errcolor = "#FFE4E1";
var wcolor = "#ffffff";
var _hdTotal = document.getElementById("total").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++){
document.getElementById("percent"+i).style.background=wcolor;
document.getElementById("amt"+i).style.background=wcolor;
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);
//document.getElementById("percent"+i).style.background=errcolor;
//document.getElementById("amt"+i).style.background=errcolor;
}
}
}
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++){
document.getElementById("percent"+i).style.background=wcolor;
document.getElementById("amt"+i).style.background=wcolor;
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);
//document.getElementById("percent"+i).style.background=errcolor;
//document.getElementById("amt"+i).style.background=errcolor;
}
}
}
}
</script>
</head>
<body>
<fieldset>
<legend align="center">จัดสรรรายจ่ายย้อนหลังในรอบ 3 เดือน</legend>
<p align="center"><? //include ("sum_amount.php");?>
<p align="center">
<form action="sav_b.php" method="POST">
<table width="523" height="129" border="1" align="center">
<tr>
<td width="60" align="center" valign="middle">ลำดับที่</td>
<td width="159" align="center" valign="middle">รายการ</td>
<td width="138" align="center" valign="middle">เปอร์เซนต์<br /></td>
<td width="138" align="center" valign="middle">จำนวนเงิน</td>
</tr>
<?php
$sum_rc="select sum(Inc_Amount) as Sum_Amount from income where Mem_ID='".$_SESSION['Mem_ID']."' ";
$query_sum=mysql_query($sum_rc);
$result = mysql_fetch_array($query_sum);
$sum_rc2="select sum(Out_Amount) as Sum_Amount from outcome where Mem_ID='".$_SESSION['Mem_ID']."' ";
$query_sum2=mysql_query($sum_rc2);
$result2 = mysql_fetch_array($query_sum2);
$a=$result['Sum_Amount'];
$b=$result2['Sum_Amount'];
$c=$a-$b;
$sum_budget="select sum(Bud_Out_Gold) as Sum_Bud from budget_outcome where Mem_ID='".$_SESSION['Mem_ID']."' ";
$sum_budget_re=mysql_query($sum_budget);
$sum_budget_result = mysql_fetch_array($sum_budget_re);
$sum_bud=$sum_budget_result['Sum_Bud'];
$b=1;
?>
<p align="center">จำนวนเงินทั้งหมด <input type="text" name="total" id="total" value="<?=$sum_bud;?>">
<?php
$table="select * from saving_type ORDER BY Sav_Type_ID ASC";
$table1=mysql_query($table);
$rows=mysql_num_rows($table1);
//echo $rows;
while($table3=mysql_fetch_assoc($table1)){
?>
<tr>
<td align="center"><?=$b;?></td>
<td align="center"><?=$table3['sav_Type_Name'];?></td>
<td align="right"><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>
</tr>
<?php
$b++;
}
?>
<tr>
<td height="45"> </td>
<td> </td>
<td align="right"><label for="textfield3"></label></td>
<td align="right"><p>
<label for="textfield"></label>
<input type="hidden" name="hdNum" id="hdNum" value="<?=$b;?>">
</p>
</td>
</tr>
</table>
</form>
<p><a href="index_user.php">กลับ</a></p>
</fieldset>
</body>
</html>
|
|
|
|
|
Date :
2013-05-18 16:37:39 |
By :
thep |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|