|
|
|
รบกวนพี่ช่วยตรวจสอบ code หน่อยครับใช้ function java แล้วระบบไม่ทำงาน |
|
|
|
|
|
|
|
จากรูป หากผมไม่ได้เลือก ข้อมูลจาก dropdown ข้อมูลต้องแสดงทั้งหมด แต่ถ้าเลือกก็จะแสดงข้อมูลที่ select ออกมา
ซึ้งตอนนี้ code น่าจะเกิด error ทำให้ ข้อมูลไม่โชว์ ไม่ทราบว่าผมเขียนตรงไหนผิดครับ
Code (PHP) select.php
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.select_data {
width: 20em !important;
margin-left: auto;
}
.table-data th {
width: 20%;
text-align: center;
}
</style>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Example</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js" integrity="sha512-STof4xm1wgkfm7heWqFJVn58Hm3EtS31XFaagaa8VMReCXAkQnJZ+jEy8PCC/iT18dFy95WcExNHFTqLyp72eQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<?php include('db_connect.php') ?>
</head>
<div class="container mt-5">
<div class="row">
<select class="form-select select_data" id="select_data">
<option value="0" selected >ข้อมูลแผนก</option>
<?php $sql = "SELECT DISTINCT department FROM data_user ORDER BY id ASC";
$result = mysqli_query($con, $sql);
$num_row = mysqli_num_rows($result);
while ($row = mysqli_fetch_assoc($result))
{ ?>
<option value="<?php echo $row['department'];?>"><?php echo $row['department']; ?></option>
<?php } ?>
</select>
<table class="table table-striped table-bordered mt-3 table-data">
<thead>
<tr class="table-primary">
<th>ลำดับ</th>
<th>ชื่อ</th>
<th>จังหวัด</th>
<th>เบอร์</th>
<th>แผนก</th>
</tr>
</thead>
<tbody id="table-body">
</tbody>
</table>
</div>
</div>
<script>
jQuery(function($) {
data_ajax(0);
$("#select_data").change(function() {
var select_data = $(this).val();
data_ajax(select_data);
});
});
function data_ajax(data) {
$.ajax({
url: "ajax-php.php",
type: "POST" ,
dataType: "json",
data: {data_id : data},
success: function(data){
$("#table-body").empty();
console.log(data);
$.each(data, function(index, record) {
var row = $("<tr></tr>");
var cell1 = $("<td class=\"text-center\">" + record.id + "</td>");
var cell2 = $("<td>" + record.username + "</td>");
var cell3 = $("<td>" + record.province + "</td>");
var cell4 = $("<td>" + record.phone_number + "</td>");
var cell5 = $("<td>" + record.department + "</td>");
row.append(cell1, cell2, cell3,cell4, cell5);
$("#table-body").append(row);
});
}
});
}
</script>
Code (PHP) ajax-php.php
<?php
include('db_connect.php');
$data_department = (isset($_POST['data_department']) && ($_POST['data_department'] != 0 )) ? $_POST['data_department'] : '';
if ($data_department != 0){
$sql = "SELECT * FROM data_user WHERE department LIKE '%" . $data_department . "%'";
} else {
$sql = "SELECT * FROM data_user WHERE department ORDER BY id ASC";
}
$result = mysqli_query($conn, $sql);
$num_row = mysqli_num_rows($result);
while ($row = mysqli_fetch_assoc($result))
{
$data[] = array( "id" => $num_row,
"username" => $row['username'],
"province" => $row['province'],
"phone_number" => $row['phone_number'],
"department" => $row['department']
);
$num_row;
}
echo json_encode($data);
mysqli_close($con);
exit();
?>
Tag : PHP
|
|
|
|
|
|
Date :
2024-11-02 16:39:55 |
By :
tharakorn.jin |
View :
109 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
กำหนดให้ php แสดง error ทุกๆอย่างเดี๋ยวจะเห็นเอง
|
|
|
|
|
Date :
2024-11-02 20:31:37 |
By :
mr.v |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ใส่ไว้บนสุดของไฟล์ครับ
Code (PHP)
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
|
|
|
|
|
Date :
2024-11-05 08:06:43 |
By :
Manussawin |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ถ้ากรณีสงสัยว่า JS error ก็ต้องเปิด browser console ดู เพราะไม่งั้นจะให้คนอื่นมาเดาก็ไม่ได้หรอก
JS JavaScript ไม่ใช่ Java มันคนละเรื่องกันนะครับ อย่าเรียกผิด (ผมเคยผิดมาแล้ว)
|
|
|
|
|
Date :
2024-11-05 08:41:34 |
By :
mr.v |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|