ต้องการให้ทั้ง 3 มันเข้าไปอยู่ใน function javascript ด้านล่าง เราควรใช้ Event ไหนครับ แล้วรูปแบบควรเขียนยังไงครับ
Code (JavaScript)
//Getting value from "ajax.php".
function fill(Value) {
//Assigning value to "search" div in "search.php" file.
$('#search').val(Value);
//Hiding "display" div in "search.php" file.
$('#display').show();
}
$(document).ready(function() {
$("#search").keyup(function() {
var name = $('#search').val();
//Validating, if "name" is empty.
if (name == "") {
//Assigning empty value to "display" div in "search.php" file.
//$("#display").html("ไม่พบข้อมูล");
$("#display").load(location.href + " #display");
}
//If name is not empty.
else {
//AJAX is called.
$.ajax({
//AJAX type is "Post".
type: "POST",
//Data will be sent to "ajax.php".
url: "ajax.php",
//Data, that will be sent to "ajax.php".
data: {
//Assigning value of "name" into "search" variable.
search: name
},
//If result found, this funtion will be called.
success: function(html) {
//Assigning result to "display" div in "search.php" file.
$("#display").html(html).show();
}
});
}
});
});
หน้า ajax.php Code (PHP)
<div class="row gutters-sm" id="display">
<div class="col-12">
<?php
//Including Database configuration file.
include "config/config_inc.php";
//Getting value of "search" variable from "script.js".
if (isset($_POST['search'])) {
//Search box value assigning to $Name variable.
$Name = $_POST['search'];
//Search query
//$Query = "SELECT tb_profile.* FROM tb_profile WHERE th_name LIKE '%$Name%' or th_sname LIKE '%$Name%' ";
$Query = "SELECT tb_profile.*,tb_contact.*,tb_data_identity.* FROM tb_profile
LEFT JOIN tb_contact ON (tb_profile.id_card =tb_contact.id_card)
LEFT JOIN tb_data_identity ON (tb_contact.id_card =tb_data_identity.id_card)
WHERE th_name LIKE '%$Name%' or th_sname LIKE '%$Name%' or phone_num LIKE '%$Name%' or blood_type LIKE '%$Name%' ";
//Query execution
$ExecQuery = MySQLi_query($con, $Query);
//Creating unordered list to display result.
//Fetching result from database.
while ($Result = MySQLi_fetch_array($ExecQuery)) {
?>
<div class="card cardmobi">
<div class="card-body ">
<span class="a"><small><?=$Result['position_number']?></small></span>
<span class="a"><small><?=$Result['position_now']?></small></span>
<span class="a"><small>สังกัด:</small></span>
<span class="a"><button class="depart btn bnt-sm btn-success"><?=$Result['department']?></button></span>
<span class="rankName"><small style="color:gray;">ยศ ชื่อ นามสกุล <br/><?=$Result['rank'];?> <?=$Result['th_name'];?> <?=$Result['th_sname'];?></small></span>
<img src="assets/img/img_profile/<?=$Result['id_card']?>.png" class="cardround"/>
</div>
</div>
<div style="height:4px"></div>
<?php
}}
?>