ThaiSEOBoard.com

พัฒนาเว็บไซต์ => Programming => ข้อความที่เริ่มโดย: mollyclick ที่ 31 กรกฎาคม 2019, 19:40:27



หัวข้อ: สอบถามการบันทึกค่า sum ใน mysql ครับ
เริ่มหัวข้อโดย: mollyclick ที่ 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-html-textbox-values-using-jquery-javascript/?unapproved=250471&moderation-hash=49be0a932ed441fe28c80373eaa291cb#comment-250471


หัวข้อ: Re: สอบถามการบันทึกค่า sum ใน mysql ครับ
เริ่มหัวข้อโดย: icez ที่ 31 กรกฎาคม 2019, 19:48:30
มันไม่ได้เป็น input ครับ มันเลยไม่ได้ส่งค่าไปหา server

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


หัวข้อ: Re: สอบถามการบันทึกค่า sum ใน mysql ครับ
เริ่มหัวข้อโดย: 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);
}

?>


หัวข้อ: Re: สอบถามการบันทึกค่า sum ใน mysql ครับ
เริ่มหัวข้อโดย: mollyclick ที่ 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 รายการเลยครับ