ยินดีต้อนรับคุณ, บุคคลทั่วไป กรุณา เข้าสู่ระบบ หรือ ลงทะเบียน

เข้าสู่ระบบด้วยชื่อผู้ใช้ รหัสผ่าน และระยะเวลาในเซสชั่น

ThaiSEOBoard.comพัฒนาเว็บไซต์Programmingสอบถามการบันทึกค่า sum ใน mysql ครับ
หน้า: [1]   ลงล่าง
พิมพ์
ผู้เขียน หัวข้อ: สอบถามการบันทึกค่า sum ใน mysql ครับ  (อ่าน 1303 ครั้ง)
0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้
mollyclick
ก๊วนเสียว
*

พลังน้ำใจ: 10
ออฟไลน์ ออฟไลน์

กระทู้: 302



ดูรายละเอียด เว็บไซต์
« เมื่อ: 31 กรกฎาคม 2019, 19:40:27 »

ตัวอย่าง 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...8c80373eaa291cb#comment-250471
« แก้ไขครั้งสุดท้าย: 31 กรกฎาคม 2019, 19:43:57 โดย mollyclick » บันทึกการเข้า

icez
Verified Seller
หัวหน้าแก๊งเสียว
*

พลังน้ำใจ: 296
ออฟไลน์ ออฟไลน์

กระทู้: 2,886



ดูรายละเอียด
« ตอบ #1 เมื่อ: 31 กรกฎาคม 2019, 19:48:30 »

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

ทำได้สองทาง
คือแก้เป็น <input type='number' id=sum name=sum> แล้วแก้ javascript ตามนิดหน่อย
กับอีกทางคือไปคำนวณบน server อีกที เอา input อื่นๆ ที่ได้มา sum กันจริงๆ
บันทึกการเข้า

THZHost SSD Hosting ไทย/สิงคโปร์ พร้อม firewall ป้องกันการยิงเว็บ + scan ไวรัสในเว็บ
smapan
Global Moderator
เจ้าพ่อบอร์ดเสียว
*****

พลังน้ำใจ: 643
ออฟไลน์ ออฟไลน์

กระทู้: 8,272



ดูรายละเอียด เว็บไซต์
« ตอบ #2 เมื่อ: 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);
}

?>
« แก้ไขครั้งสุดท้าย: 31 กรกฎาคม 2019, 20:06:46 โดย smapan » บันทึกการเข้า

จูมล่าโฮส สยามโฮสเว็บ modty.com
รวมที่พัก เช่ารายวัน ที่พักเช่ารายเดือนมากที่สุดแจ่มจริง
***Tel 083-757-1515 ติดปัญหา Joomla ตรงไหนรับปรึกษาฟรี โทรมาเถอะครับ ถ้าตอบได้ช่วยแน่นอน ไม่มีกั้ก. ***
mollyclick
ก๊วนเสียว
*

พลังน้ำใจ: 10
ออฟไลน์ ออฟไลน์

กระทู้: 302



ดูรายละเอียด เว็บไซต์
« ตอบ #3 เมื่อ: 01 สิงหาคม 2019, 09:50:06 »

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 รายการเลยครับ
บันทึกการเข้า

หน้า: [1]   ขึ้นบน
พิมพ์