Mình nghĩ thế này ko biết đúng ko?
- method của form của mình là POST, còn của ajax là GET. Mình đang lấy từ form nên mình dùng POST
- data gửi sang có name đó mà bạn, do code dài nên mình chỉ chụp 1 đoạn
Nếu có thời gian thì bạn xem qua mình với!
Ngoài ra code còn yếu phần nào mong được mn góp ý
Đây là full code:
<?php include '../../classes/workday.php' ?>
<?php
$workday = new Workday();
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if (isset($_POST['btnSubmit'])) // Kiểm tra nút có giá trị dữ liệu
{
if($_POST['btnSubmit'] == '+')
{
$year = $_POST['year'];
$insert = $workday->insert_year($year);
}
if($_POST['btnSubmit'] == 'Insert All')
{
$size = $_POST['size'];
$workdayMonth = $_POST["workdayMonth"];
$workdayYear = $_POST["workdayYear"];
for ($i = 1; $i <= 6; $i++){
$employeeId = $_POST["employeeId".$i];
$dayWorked = $_POST["dayWorked".$i];
$overTime = $_POST["overTime".$i];
$insert = $workday->insert_workday($employeeId, $dayWorked, $overTime, $workdayMonth, $workdayYear);
}
}
}
}
?>
<?php include '../../inc/header.php' ?>
<div class="body row">
<?php include '../../inc/sidebar.php' ?>
<div class="content col-10 col-sm-9 col-xl-10">
<form class="insert-year" method="post" action="">
<table class="mt-3">
<tr>
<td><input type="text" class="form-control" name="year" size="5" placeholder="Year"></td>
<td>----</td>
<td><input type="submit" class="btn btn-success" name="btnSubmit" value="+"></td>
</tr>
</table>
</form>
<div class="filter">
<h3 class="text-center display-block">Filter</h3>
<table class="table">
<thead class="thead-light">
<tr>
<th scope="col">Employee Id</th>
<th scope="col">FullName</th>
<th scope="col">Department</th>
<th scope="col">Position</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<td>
<input type="text" id="workday_employeeId" name="workday_employeeId">
</td>
<td>
<input type="text" id="workday_employee" name="workday_employee">
</td>
<td>
<select class="form-control col-sm-8" id="workday_department" name="workday_department">
<option selected value="">All</option>
<?php
$show = $workday->show('t_Department');
if($show){
while($result = $show->fetch_assoc()){
?>
<option value="<?php echo $result['Department_id']?>"><?php echo $result['Department_id']." - ".$result['Department_name']; ?></option>
<?php
}
}
?>
</select>
</td>
<td>
<select class="form-control col-sm-8" id="workday_position" name="workday_position">
<option selected value="">All</option>
<?php
$show = $workday->show('t_Position');
if($show){
while($result = $show->fetch_assoc()){
?>
<option value="<?php echo $result['Position_id']?>"><?php echo $result['Position_id']." - ".$result['Position_name']; ?></option>
<?php
}
}
?>
</select>
</td>
<td>
<button id="btn-filter" class="btn btn-success">Filter</button>
</td>
</tbody>
</table>
</div>
<h3 class="text-center display-block">Insert Workday Infomation</h3>
<?php
if(isset($insert)){
echo $insert;
}
?>
<form id="insert-workday" class="mx-auto" action="" method="post">
<table style="margin-bottom: 10px">
<tr>
<td style="padding-right: 10px">
<label>Year:</label>
</td>
<td style="width: 150px">
<select class="form-control col-sm-8" id="worday_year" name="workdayYear">
<option selected value="">All</option>
<?php
$show = $workday->show('t_Salary_year');
if($show){
while($result = $show->fetch_assoc()){
?>
<option value="<?php echo $result['Salary_year']?>"><?php echo $result['Salary_year']?></option>
<?php
}
}
?>
</select>
</td>
<td style="padding-right: 10px">
<label>Month:</label>
</td>
<td style="width: 100px">
<select class="form-control col-sm-8" id="workday_month" name="workdayMonth">
<option selected value="">All</option>
<?php
for ($i=1; $i<=12; $i++){
?>
<option value="<?php echo $i?>"><?php echo $i ?></option>
<?php
}
?>
</select>
</td>
</tr>
</table>
<table id="add-workday" class="table table-striped">
<thead class="thead-dark">
<tr>
<th scope="col">Ordinal</th>
<th scope="col">Employee Id</th>
<th scope="col">Fullname</th>
<th scope="col">Department</th>
<th scope="col">Position</th>
<th scope="col">Day worked</th>
<th scope="col">Overtime</th>
</tr>
</thead>
<tbody id="body_add_workday">
<!-- Chèn nội dung ajax ở đây -->
</tbody>
</table>
<input type="submit" class="btn btn-success" name="btnSubmit" value="Insert All">
<nav aria-label="Page navigation example">
<ul class="pagination justify-content-center" id="workday-pagenumber">
<!-- Chèn nội dung ajax ở đây -->
</ul>
</nav>
</form>
</div>
</div>
<script>
var index = 1;
var amount = 5;
var btnFilter = $("#btn-filter");
$(document).ready(function(){
var workday_employee = $("#workday_employee").val();
var workday_department = $("#workday_department").val();
var workday_position = $("#workday_position").val();
var workday_employeeId = $("#workday_employeeId").val();
$.get("page_add.php",
{
page:index,
amount,
workday_employeeId: workday_employeeId,
workday_employee: workday_employee,
workday_department: workday_department,
workday_position: workday_position
},
function(data){
$("#body_add_workday").append(data);
});
$.get("page_number_add.php",
{
amount,
workday_employeeId: workday_employeeId,
workday_employee: workday_employee,
workday_department: workday_department,
workday_position: workday_position
},
function(data){
$("#workday-pagenumber").html(data);
});
// Cần nav.on() để load lại nav(nav là cha của page-link) trang sau khi gọi ajax
$('nav').on('click','.page-link',function(e){
e.preventDefault();
var workday_employee = $("#workday_employee").val();
var workday_department = $("#workday_department").val();
var workday_position = $("#workday_position").val();
index = Number($(this).text());
$("li").removeClass('active');
$(this).parent().addClass('active');
$.get("page_add.php",
{
page:index,
amount,
workday_employeeId: workday_employeeId,
workday_employee: workday_employee,
workday_department: workday_department,
workday_position: workday_position
},
function(data){
$("#body_add_workday").html(data);
});
});
$("#btn-filter").click(function(){
index = 1;
var workday_employee = $("#workday_employee").val();
var workday_department = $("#workday_department").val();
var workday_position = $("#workday_position").val();
$.get("page_add.php",
{
page:index,
amount,
workday_employeeId: workday_employeeId,
workday_employee: workday_employee,
workday_department: workday_department,
workday_position: workday_position
},
function(data){
$("#body_add_workday").html(data);
});
$.get("page_number_add.php",
{
amount,
workday_employeeId: workday_employeeId,
workday_employee: workday_employee,
workday_department: workday_department,
workday_position: workday_position
},
function(data){
$("#workday-pagenumber").html(data);
});
});
});
</script>
<?php include '../../inc/footer.php' ?>
- page_add.php (nó là trang page.php mà mình nói ở trên)
<?php
include '../../lib/database.php';
$database = new Database();
$where = $_GET['page'];
$amount =$_GET['amount'];
$employeeId = empty($_GET['workday_employeeId'])?'%':$_GET['workday_employeeId'];
$fullname = empty($_GET['workday_employee'])?'%':$_GET['workday_employee'];
$department = empty($_GET['workday_department'])?'%':$_GET['workday_department'];
$position = empty($_GET['workday_position'])?'%':$_GET['workday_position'];
settype($page,"int");
$start = ($where - 1) * $amount;
$rs = $database->filter_employee_limit($employeeId, $fullname, '%', $department, $position, '%', '%', '%', '30000101', '1', $start, $amount);
$i = $start;
if ($rs){
while($row = $rs->fetch_assoc()){
$i++;
echo "
<tr>
<td class='py-3'>".$i."</td>
<td class='py-3' value='".$row["Employee_id"]."' name='employeeId".$i."'>".$row["Employee_id"]."</td>
<td class='py-3'>".$row["Fullname"]."</td>
<td class='py-3'>".$row["Department_name"]."</td>
<td class='py-3'>".$row["Position_name"]."</td>
<td class='py-3'><input type=text name='dayworked".$i."' size=5 class='form-control'></td>
<td class='py-3'><input type=text name='overtime".$i."' size=5 class='form-control'></td>
</tr>
";
}
}
echo "<input type='text' name='size' value='".$amount."'>";
?>
Ghi chú: file page_number_add.php chỉ để đánh số trang
<?php
include '../../lib/database.php';
$database = new Database();
$amount =$_GET['amount'];
$id = empty($_GET['workday_employeeId'])?'%':$_GET['workday_employeeId'];
$employee = empty($_GET['workday_employee'])?'%':$_GET['workday_employee'];
$department = empty($_GET['workday_department'])?'%':$_GET['workday_department'];
$position = empty($_GET['workday_position'])?'%':$_GET['workday_position'];
$rs = $database->filter_employee($id, $employee, '%', $department, $position, '%', '%', '%', '30000101', '1');
if ($rs){
$num = $rs->num_rows;
$totalPage = ceil($num / $amount);
echo "<li class='page-item active'><button class='page-link'>1</button></li>";
for ($i = 1;$i<$totalPage;$i++){
echo "<li class='page-item'><button class='page-link'>".($i+1)."</button></li>";
}
}
else{
echo "<p class='alert alert-warning'>Khong co du lieu</p>";
}
?>
Mặt dày ăn chửi để còn tiến bộ