สอบถามการบันทึกค่า sum ใน mysql ครับ

เริ่มโดย mollyclick, 31 กรกฎาคม 2019, 19:40:27

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

mollyclick

ตัวอย่าง code เราจะเอาค่า sum ไหนไปบันทึก

<span id="sum">0</span>  ผมเอา id="sum" ในโค้ดนี้ไปแล้วมันบันทึกไม่ได้

ต้องทำยังไงถึงเอาไปบันทึกลง sql ได้ครับ
:wanwan017:

<html>
<head>
<title>Sum Html Textbox Values using jQuery/JavaScript</title>
<style>
body {
font-family: sans-serif;
}
#summation {
font-size: 18px;
font-weight: bold;
color:#174C68;
}
.txt {
background-color: #FEFFB0;
font-weight: bold;
text-align: right;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<table width="300px" border="1" style="border-collapse:collapse;background-color:#E8DCFF">
<tr>
<td width="40px">1</td>
<td>Butter</td>
<td><input class="txt" type="text" name="txt"/></td>
</tr>
<tr>
<td>2</td>
<td>Cheese</td>
<td><input class="txt" type="text" name="txt"/></td>
</tr>
<tr>
<td>3</td>
<td>Eggs</td>
<td><input class="txt" type="text" name="txt"/></td>
</tr>
<tr>
<td>4</td>
<td>Milk</td>
<td><input class="txt" type="text" name="txt"/></td>
</tr>
<tr>
<td>5</td>
<td>Bread</td>
<td><input class="txt" type="text" name="txt"/></td>
</tr>
<tr>
<td>6</td>
<td>Soap</td>
<td><input class="txt" type="text" name="txt"/></td>
</tr>
<tr id="summation">
<td>&nbsp;</td>
<td align="right">Sum :</td>
<td align="center"><span id="sum">0</span></td>
</tr>
</table>


<script>
$(document).ready(function(){

//iterate through each textboxes and add keyup
//handler to trigger sum event
$(".txt").each(function() {

$(this).keyup(function(){
calculateSum();
});
});

});

function calculateSum() {

var sum = 0;
//iterate through each textboxes and add the values
$(".txt").each(function() {

//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum  = parseFloat(this.value);
}

});
//.toFixed() method will roundoff the final sum to 2 decimal places
$("#sum").html(sum.toFixed(2));
}
</script>
</body>
</html>


ที่มาจาก : https://www.viralpatel.net/sum-html-textbox-values-using-jquery-javascript/?unapproved=250471&moderation-hash=49be0a932ed441fe28c80373eaa291cb#comment-250471

icez

มันไม่ได้เป็น input ครับ มันเลยไม่ได้ส่งค่าไปหา server

ทำได้สองทาง
คือแก้เป็น <input type='number' id=sum name=sum> แล้วแก้ javascript ตามนิดหน่อย
กับอีกทางคือไปคำนวณบน server อีกที เอา input อื่นๆ ที่ได้มา sum กันจริงๆ
[direct=http://www.thzhost.com/]THZHost[/direct] SSD Hosting ไทย/สิงคโปร์ พร้อม firewall ป้องกันการยิงเว็บ + scan ไวรัสในเว็บ

smapan

#2
test
function calculateSum() {

var sum = 0;
//iterate through each textboxes and add the values
$(".txt").each(function() {

//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum  = parseFloat(this.value);
}

});
//.toFixed() method will roundoff the final sum to 2 decimal places
$("#sum").html(sum.toFixed(2));
$.ajax({
           type: 'POST',
          url: window.location.pathname,
           data: {'sum':$("#sum").text},          
           success: function(resultData) { alert("Save Complete") }
       });
}
<?php
if(isset($_POST["sum"])){
$servername "localhost";
$username "username";

$password "password";
$dbname "myDB";
$tablename "myTable";
$feildname "myFeild";
$sum $_POST["sum"];

// Create connection
$conn mysqli_connect($servername$username$password$dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " mysqli_connect_error());
}

$sql "INSERT INTO $tablename ($feildname)
VALUES ('
$sum')";
mysqli_query($conn$sql)) 

mysqli_close($conn);
}

?>
[direct=http://cp.siamhostweb.com/cart.php?gid=1]จูมล่าโฮส สยามโฮสเว็บ[/direct] [direct=http://www.modty.com]modty.com[/direct]
[direct=http://www.apartment.in.th]รวมที่พัก เช่ารายวัน ที่พักเช่ารายเดือนมากที่สุดแจ่มจริง[/direct]
***Tel 083-757-1515 ติดปัญหา Joomla ตรงไหนรับปรึกษาฟรี โทรมาเถอะครับ ถ้าตอบได้ช่วยแน่นอน ไม่มีกั้ก. ***

mollyclick

อ้างถึงจาก: smapan ใน 31 กรกฎาคม 2019, 20:05:01
test
function calculateSum() {

var sum = 0;
//iterate through each textboxes and add the values
$(".txt").each(function() {

//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum  = parseFloat(this.value);
}

});
//.toFixed() method will roundoff the final sum to 2 decimal places
$("#sum").html(sum.toFixed(2));
$.ajax({
           type: 'POST',
          url: window.location.pathname,
           data: {'sum':$("#sum").text},          
           success: function(resultData) { alert("Save Complete") }
       });
}
<?php
if(isset($_POST["sum"])){
$servername "localhost";
$username "username";

$password "password";
$dbname "myDB";
$tablename "myTable";
$feildname "myFeild";
$sum $_POST["sum"];

// Create connection
$conn mysqli_connect($servername$username$password$dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " mysqli_connect_error());
}

$sql "INSERT INTO $tablename ($feildname)
VALUES ('
$sum')";
mysqli_query($conn$sql)) 

mysqli_close($conn);
}

?>


มันบันทึกค่า ว่างเปล่า 7 รายการเลยครับ