โดย Parichat » 10/01/2018 2:54 pm
การค้นหาข้อมูลโดยใช้ AJAX PHP เป็นการสื่อสารกันระหว่างเว็บเพจกับเว็บเซิร์ฟเวอร์มาดูตัวอย่างต่อไปนี้จะแสดงให้เห็นว่าเว็บเพจสามารถสื่อสารกับเว็บเซิร์ฟเวอร์ได้อย่างไรในขณะที่ user พิมพ์ตัวอักษรลงใน text box
ตัวอย่าง
ต้องสร้างไฟล์อก1ไฟล์ ชื่อว่า
gethint.php
และใส่โค้ดนี้ลงไป
โค้ด: เลือกทั้งหมด
<?php
// Array with names
$a[] = "Anna";
$a[] = "Brittany";
$a[] = "Cinderella";
$a[] = "Diana";
$a[] = "Eva";
$a[] = "Fiona";
$a[] = "Gunda";
$a[] = "Hege";
$a[] = "Inga";
$a[] = "Johanna";
$a[] = "Kitty";
$a[] = "Linda";
$a[] = "Nina";
$a[] = "Ophelia";
$a[] = "Petunia";
$a[] = "Amanda";
$a[] = "Raquel";
$a[] = "Cindy";
$a[] = "Doris";
$a[] = "Eve";
$a[] = "Evita";
$a[] = "Sunniva";
$a[] = "Tove";
$a[] = "Unni";
$a[] = "Violet";
$a[] = "Liza";
$a[] = "Elizabeth";
$a[] = "Ellen";
$a[] = "Wenche";
$a[] = "Vicky";
// get the q parameter from URL
$q = $_REQUEST["q"];
$hint = "";
// lookup all hints from array if $q is different from ""
if ($q !== "") {
$q = strtolower($q);
$len=strlen($q);
foreach($a as $name) {
if (stristr($q, substr($name, 0, $len))) {
if ($hint === "") {
$hint = $name;
} else {
$hint .= ", $name";
}
}
}
}
// Output "no suggestion" if no hint was found or output correct values
echo $hint === "" ? "no suggestion" : $hint;
?>
จากนั้นเราก็สร้างอกหน้านึงเพื่อที่จะทำฟอร์ม มีตัวอย่างโค้ดดังนี้
โค้ด: เลือกทั้งหมด
<!DOCTYPE html>
<html>
<head>
<script>
function showHint(str) {
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
}
xmlhttp.open("GET", "gethint.php?q="+str, true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<p><b>สามารถค้นหาชื่อในช่องนี้:</b></p>
<form>
ชื่อ: <input type="text" onkeyup="showHint(this.value)">
</form>
<p>ผลลัพธ์: <span id="txtHint"></span></p>
</body>
</html>
ผลลัพธ์
- dgh.JPG (11.68 KiB) Viewed 2388 times
อ้างอิง :
https://www.w3schools.com/php/php_ajax_php.asp
[b][size=150][color=#FF00BF]การค้นหาข้อมูลโดยใช้ [url=https://www.mindphp.com/forums/viewforum.php?f=78]AJAX[/url] [url=https://www.mindphp.com/forums/viewforum.php?f=72]PHP[/color][/size][/url][/b] เป็นการสื่อสารกันระหว่างเว็บเพจกับเว็บเซิร์ฟเวอร์มาดูตัวอย่างต่อไปนี้จะแสดงให้เห็นว่าเว็บเพจสามารถสื่อสารกับเว็บเซิร์ฟเวอร์ได้อย่างไรในขณะที่ user พิมพ์ตัวอักษรลงใน text box
[color=#FF0000][b]ตัวอย่าง[/b][/color]
ต้องสร้างไฟล์อก1ไฟล์ ชื่อว่า [quote][b]gethint.php[/b][/quote]และใส่โค้ดนี้ลงไป
[code]<?php
// Array with names
$a[] = "Anna";
$a[] = "Brittany";
$a[] = "Cinderella";
$a[] = "Diana";
$a[] = "Eva";
$a[] = "Fiona";
$a[] = "Gunda";
$a[] = "Hege";
$a[] = "Inga";
$a[] = "Johanna";
$a[] = "Kitty";
$a[] = "Linda";
$a[] = "Nina";
$a[] = "Ophelia";
$a[] = "Petunia";
$a[] = "Amanda";
$a[] = "Raquel";
$a[] = "Cindy";
$a[] = "Doris";
$a[] = "Eve";
$a[] = "Evita";
$a[] = "Sunniva";
$a[] = "Tove";
$a[] = "Unni";
$a[] = "Violet";
$a[] = "Liza";
$a[] = "Elizabeth";
$a[] = "Ellen";
$a[] = "Wenche";
$a[] = "Vicky";
// get the q parameter from URL
$q = $_REQUEST["q"];
$hint = "";
// lookup all hints from array if $q is different from ""
if ($q !== "") {
$q = strtolower($q);
$len=strlen($q);
foreach($a as $name) {
if (stristr($q, substr($name, 0, $len))) {
if ($hint === "") {
$hint = $name;
} else {
$hint .= ", $name";
}
}
}
}
// Output "no suggestion" if no hint was found or output correct values
echo $hint === "" ? "no suggestion" : $hint;
?>[/code]
จากนั้นเราก็สร้างอกหน้านึงเพื่อที่จะทำฟอร์ม มีตัวอย่างโค้ดดังนี้
[code]<!DOCTYPE html>
<html>
<head>
<script>
function showHint(str) {
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
}
xmlhttp.open("GET", "gethint.php?q="+str, true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<p><b>สามารถค้นหาชื่อในช่องนี้:</b></p>
<form>
ชื่อ: <input type="text" onkeyup="showHint(this.value)">
</form>
<p>ผลลัพธ์: <span id="txtHint"></span></p>
</body>
</html>[/code]
[color=#FF0000][b]ผลลัพธ์[/b][/color]
[attachment=0]dgh.JPG[/attachment]
อ้างอิง : [url]https://www.w3schools.com/php/php_ajax_php.asp[/url]