|
 |
|
ช่วยทีครับทำ textbox รับค่าแล้วแสดง textbox ให้กรอกข้อมูลเพื่อคำนวณครับ |
|
 |
|
|
 |
 |
|
เหมือนเจ้าของกระทู้จะรับค่าจำนวนแถว แล้วใช้วิธีซับมิท
วนลูปแสดงแถวด้วย php ใช่หรือป่าว ขอโค้ดมาดูครับ
|
 |
 |
 |
 |
Date :
2014-11-21 08:01:54 |
By :
cowboycnx |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<!DOCTYPE HTML>
<html>
<head>
<title>คำนวณไฟฟ้าl</title>
<style type="text/css">
body,td,th {
color: #FFF;
}
body {
background-color: #000;
}
</style>
</head>
<body>
<meta charset="utf-8">
<script language="javascript">
function fncCal()
{
var tot = 0;
var sum = 0;
for(i=1;i<=document.form1.hdnLine.value;i++)
{
tot = parseInt(eval("document.form1.txtVol1_"+i+".value")) * parseInt(eval("document.form1.txtVol2_"+i+".value"))*30/1000
eval("document.form1.txtVol3_"+i+".value="+tot);
sum = tot + sum
document.form1.txtSum.value=sum;
}
}
</script>
<form action="cal.php" method="post" name="form1">
<p> </p>จำนวนเครื่องใช้ไฟฟ้า
<p>เครื่องใช้ไฟฟ้า 1
<input name="txtVol1_" id="txtVol1_1" type="text">
<input name="txtVol[]" id="txtVol2_1" type="text"> = <input name="txtVol[]" id="txtVol3_1" type="text"><br>
เครื่องใช้ไฟฟ้า 2
<input name="txtVol[]" id="txtVol1_2" type="text">
<input name="txtVol[]" id="txtVol2_2" type="text"> = <input name="txtVol[]" id="txtVol3_2" type="text"><br>
เครื่องใช้ไฟฟ้า 3
<input name="txtVol[]" id="txtVol1_3" type="text">
<input name="txtVol[]" id="txtVol2_3" type="text"> = <input name="txtVol[]" id="txtVol3_3" type="text"><br>
เครื่องใช้ไฟฟ้า 4
<input name="txtVol[]" id="txtVol1_4" type="text">
<input name="txtVol[]" id="txtVol2_4" type="text"> = <input name="txtVol[]" id="txtVol3_4" type="text"><br>
เครื่องใช้ไฟฟ้า 5
<input name="txtVol[]" id="txtVol1_5" type="text">
<input name="txtVol[]" id="txtVol2_5" type="text"> = <input name="txtVol[]" id="txtVol3_5" type="text"><br>
<input type="hidden" name="hdnLine" value="5">
จำนวนหน่วย:
<input name="txtSum" id="txtSum" type="text"><br><br>
<input name="btnCal" type="button" value="คำนวณ" OnClick="JavaScript:fncCal();">
</p>
</form>
<a href="sumpea_price" target="index">คำนวณค่าใช้ไฟฟ้า</a>
</body>
</html>
ตอนนี้ผมทำแบบนี้ครับผมพึ่งศึกษาครับไม่ค่อยมีความรู้แต่มีคนให้โค้ดอีกอันมามีรับค่าแต่ผมเขียนคำนวณไม่เป็นครับ
Code (PHP)
<?php
if (empty($_POST[counttextbox])) {
echo "<form name='frm1' action='text_result.php' method='post'>";
echo "กรุณากรอกจำนวนที่ต้องการคำนวณ";
echo "<br/>";
echo "<input type='text' name='counttextbox' size='5' maxlength='10' onKeyUp='if(this.value*1!=this.value) this.value='' ;'>";
echo "<input type='submit' value='Submit'>";
echo "<form/>";
} else {
$alltext=floor($_POST[counttextbox]);
echo "<form name='frm1' action='' method='post'>";
echo "แสดงผล <br/>";
for ($loop=0; $loop<$alltext; $loop++) {
echo "<input type='text' name='inputtext1[]' id='inputtext1[]' size='7' maxlength='10' onKeyUp='if(this.value*1!=this.value) this.value='' ;'> x ";
echo "<input type='text' name='inputtext2[]' id='inputtext2[]' size='7' maxlength='10' onKeyUp='if(this.value*1!=this.value) this.value='' ;'> x ";
echo "<input type='text' name='inputtext3[]' id='inputtext3[]' size='7' maxlength='10' onKeyUp='if(this.value*1!=this.value) this.value='' ;'> = ";
echo "<input type='text' name='outputtext1[]' id='outputtext1[]' size='7' maxlength='10' readonly='readonly'>";
echo "<br/><br/>";
}
echo "<input type='submit' value='Submit'>";
echo "<form/>";
}
?>
|
 |
 |
 |
 |
Date :
2014-11-21 17:08:28 |
By :
babavab |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
   
|
 |
 |
 |
 |
Date :
2014-11-21 23:42:26 |
By :
babavab |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมไม่ได้ทดสอบน่ะครับ ไปลองรันดูเอาเอง
<?php
if (empty($_POST[counttextbox])) {
echo "<form name='frm1' action='text_result.php' method='post'>";
echo "กรุณากรอกจำนวนที่ต้องการคำนวณ";
echo "<br/>";
echo "<input type='text' name='counttextbox' size='5' maxlength='10' onKeyUp='if(this.value*1!=this.value) this.value='' ;'>";
echo "<input type='submit' value='Submit'>";
echo "<form/>";
} else {
$alltext=floor($_POST[counttextbox]);
echo "<form name='frm1' action='' method='post'>";
echo "แสดงผล <br/>";
for ($loop=0; $loop<$alltext; $loop++) {
echo "<div>";
echo "<input type='text' name='inputtext1[]' class='multiply input1' size='7' maxlength='10' > x ";
echo "<input type='text' name='inputtext2[]' class='multiply input2' size='7' maxlength='10' > x ";
echo "<input type='text' name='inputtext3[]' class='multiply input3' size='7' maxlength='10' > = ";
echo "<input type='text' name='outputtext[]' class='output' size='7' maxlength='10' readonly='readonly'>";
echo "</div><br/>";
}
echo "<input type='submit' value='Submit'>";
echo "<form/>";
}
?>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".multiply").keyup(function(){
var parent = $(this).parent();
var num1 = parent.find(".input1").val(),
num2 = parent.find(".input2").val(),
num3 = parent.find(".input3").val();
parent.find(".output").val(parseFloat(num1)*parseFloat(num2)*parseFloat(num3));
});
});
</script>
อาจจะเจอปัญหาตอนที่ช่องไหนไม่มีข้อมูล มันก็จะขึ้น undefined
|
 |
 |
 |
 |
Date :
2014-11-22 07:55:57 |
By :
cowboycnx |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับคำนวณได้แล้ว แต่มันเป็นคำนวณอัตโนมัติไม่ต้องกด submit แล้วผมจะเพิ่มอีก 1 ช่องคือทำเป็นรวมผลลัพที่คำนวณต้องทำยังไงหรอครับ
|
 |
 |
 |
 |
Date :
2014-11-22 16:46:53 |
By :
babavab |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่คิดจะศึกษาโค๊ดที่เค้าให้ไปบ้างเลยหรอ
|
 |
 |
 |
 |
Date :
2014-11-22 17:08:44 |
By :
ไม่เห็นจะพยายามอะไรเลย |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอโทษนะครับที่ต้องให้ลำบาก ผมลองทำตามหลายตัวอย่างแล้วแต่มันทำไม่ได้ผมเลยถามครับ
|
 |
 |
 |
 |
Date :
2014-11-23 02:33:04 |
By :
babavab |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันทำได้หลายวิธีน่ะครับ
<?php
if (empty($_POST[counttextbox])) {
echo "<form name='frm1' action='text_result.php' method='post'>";
echo "กรุณากรอกจำนวนที่ต้องการคำนวณ";
echo "<br/>";
echo "<input type='text' name='counttextbox' size='5' maxlength='10' onKeyUp='if(this.value*1!=this.value) this.value='' ;'>";
echo "<input type='submit' value='Submit'>";
echo "<form/>";
} else {
$alltext=floor($_POST[counttextbox]);
echo "<form name='frm1' action='' method='post'>";
echo "แสดงผล <br/>";
for ($loop=0; $loop<$alltext; $loop++) {
echo "<div>";
echo "<input type='text' name='inputtext1[]' class='multiply input1' size='7' maxlength='10' > x ";
echo "<input type='text' name='inputtext2[]' class='multiply input2' size='7' maxlength='10' > x ";
echo "<input type='text' name='inputtext3[]' class='multiply input3' size='7' maxlength='10' > = ";
echo "<input type='text' name='outputtext[]' class='output' size='7' maxlength='10' readonly='readonly'>";
echo "</div><br/>";
}
echo "<input type='text' name='grandtotal' id='grandtotal' value='' /><br />";
echo "<input type='submit' value='Submit'>";
echo "<form/>";
}
?>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".multiply").keyup(function(){
var parent = $(this).parent();
var num1 = parent.find(".input1").val(),
num2 = parent.find(".input2").val(),
num3 = parent.find(".input3").val();
parent.find(".output").val(parseFloat(num1)*parseFloat(num2)*parseFloat(num3));
var gtotal = 0;
$("input.output").each(function(){
gtotal = gtotal + parseFloat($(this).val());
});
$("#grandtotal").val(gtotal);
});
});
</script>
ชุดนี้ดีหน่อยไม่ต้องคำนวนทุกครั้งที่มีการกดคีย์ แต่จะคำนวนต่อเมื่อคลิ๊กเมาส์นอกช่องกรอกข้อมูล
<?php
if (empty($_POST[counttextbox])) {
echo "<form name='frm1' action='text_result.php' method='post'>";
echo "กรุณากรอกจำนวนที่ต้องการคำนวณ";
echo "<br/>";
echo "<input type='text' name='counttextbox' size='5' maxlength='10' onKeyUp='if(this.value*1!=this.value) this.value='' ;'>";
echo "<input type='submit' value='Submit'>";
echo "<form/>";
} else {
$alltext=floor($_POST[counttextbox]);
echo "<form name='frm1' action='' method='post'>";
echo "แสดงผล <br/>";
for ($loop=0; $loop<$alltext; $loop++) {
echo "<div>";
echo "<input type='text' name='inputtext1[]' class='multiply input1' size='7' maxlength='10' > x ";
echo "<input type='text' name='inputtext2[]' class='multiply input2' size='7' maxlength='10' > x ";
echo "<input type='text' name='inputtext3[]' class='multiply input3' size='7' maxlength='10' > = ";
echo "<input type='text' name='outputtext[]' class='output' size='7' maxlength='10' readonly='readonly'>";
echo "</div><br/>";
}
echo "<input type='text' name='grandtotal' id='grandtotal' value='' /><br />";
echo "<input type='submit' value='Submit'>";
echo "<form/>";
}
?>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".multiply").keyup(function(){
var parent = $(this).parent();
var num1 = parent.find(".input1").val(),
num2 = parent.find(".input2").val(),
num3 = parent.find(".input3").val();
parent.find(".output").val(parseFloat(num1)*parseFloat(num2)*parseFloat(num3));
}).blur(function(){
var gtotal = 0;
$("input.output").each(function(){
gtotal = parseFloat(gtotal) + parseFloat($(this).val());
});
$("#grandtotal").val(gtotal);
});
});
</script>
โค้ดพวกนี้อาจจะยากไปสำหรับมือใหม่ครับ พยายามศึกษา อ่านตัวอย่างโค้ดและทำความเข้าใจให้ดีครับ 
|
 |
 |
 |
 |
Date :
2014-11-23 09:01:12 |
By :
cowboycnx |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากครับคุณ cowboycnx ผมมือใหม่จริงๆ ขอบคุณมากๆครับที่เข้าใจ ผมนั่งทำทั้งวันมันก็ไม่ออก
|
 |
 |
 |
 |
Date :
2014-11-23 19:55:15 |
By :
babavab |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 00
|