ผมมี source code Ajax มันจะเพิ่มข้อมูลเข้า combobox ทุกครั้งที่ทำการ refresh ตัวเองไม่ยอมหยุดคับ ใครพอมีวิธีทำให้มัน refresh แค่ครั้งเดียวบ้างมั้ยคับ source code มีดังนี้คับ
นี่คือไฟล์ getdata.php
<?php
ini_set('display_errors', 1);
error_reporting(~0);
$serverName = "localhost";
$userName = "root";
$userPassword = "";
$dbName = "people";
$conn = mysqli_connect($serverName,$userName,$userPassword,$dbName);
$sql = "SELECT * FROM supplies";
$query = mysqli_query($conn,$sql);
if (!$query) {
printf("Error: %s\n", $conn->error);
exit();
}
$resultArray = array();
while($result = mysqli_fetch_array($query,MYSQLI_ASSOC))
{
array_push($resultArray,$result);
}
mysqli_close($conn);
echo json_encode($resultArray);
?>
นี่คือไฟล์ index.php
<!DOCTYPE html>
<html>
<head>
<title>ThaiCreate.Com</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="
http://code.jquery.com/jquery-latest.js"></script>
<meta charset=utf-8 />
<script>
function getDataFromDb()
{
$.ajax({
url: "getData.php" ,
type: "POST",
data: ''
})
.success(function(result) {
var obj = jQuery.parseJSON(result);
if(obj != '')
{
//$("#myTable tbody tr:not(:first-child)").remove();
$("#myBody").empty();
$.each(obj, function(key, val) {
var tr = "<option>";
tr = tr + val["supplyerID"] + " ";
tr = tr + val["supplyer_name"];
tr = tr + "</option>";
$("#select1").append(new Option(val["supplyer_name"], val["supplyerID"]));
});
}
});
}
setInterval(getDataFromDb,3000); // 1000 = 1 second
</script>
</head>
<body>
<center>
<h1>My Web</h1>
<select id="select1">
</select>
<center>
</body>
</html>
อยากถามท่านผู้รู้เรื่องการใช้งาน AJAX หน่อยคับ
นี่คือไฟล์ getdata.php
<?php
ini_set('display_errors', 1);
error_reporting(~0);
$serverName = "localhost";
$userName = "root";
$userPassword = "";
$dbName = "people";
$conn = mysqli_connect($serverName,$userName,$userPassword,$dbName);
$sql = "SELECT * FROM supplies";
$query = mysqli_query($conn,$sql);
if (!$query) {
printf("Error: %s\n", $conn->error);
exit();
}
$resultArray = array();
while($result = mysqli_fetch_array($query,MYSQLI_ASSOC))
{
array_push($resultArray,$result);
}
mysqli_close($conn);
echo json_encode($resultArray);
?>
นี่คือไฟล์ index.php
<!DOCTYPE html>
<html>
<head>
<title>ThaiCreate.Com</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<meta charset=utf-8 />
<script>
function getDataFromDb()
{
$.ajax({
url: "getData.php" ,
type: "POST",
data: ''
})
.success(function(result) {
var obj = jQuery.parseJSON(result);
if(obj != '')
{
//$("#myTable tbody tr:not(:first-child)").remove();
$("#myBody").empty();
$.each(obj, function(key, val) {
var tr = "<option>";
tr = tr + val["supplyerID"] + " ";
tr = tr + val["supplyer_name"];
tr = tr + "</option>";
$("#select1").append(new Option(val["supplyer_name"], val["supplyerID"]));
});
}
});
}
setInterval(getDataFromDb,3000); // 1000 = 1 second
</script>
</head>
<body>
<center>
<h1>My Web</h1>
<select id="select1">
</select>
<center>
</body>
</html>