我想在一个带有下拉列表的表中显示用户数据,第一个选项显示所有用户,但是当我单击一个名称时,它仍然显示所有用户,我认为我的ajax脚本有一些问题,因为它似乎不起作用
这是我显示数据的页面:
<?php
//load_data_select.php
$connect = mysqli_connect("localhost", "root", "ntr-ktb123", "absence");
function fill_emp($connect)
{
$output = '';
$sql = "SELECT * FROM employés";
$result = mysqli_query($connect, $sql);
while($row = mysqli_fetch_array($result))
{
$output = $output."'<option value="'.$row["IdEmp"].'">'.$row["NomEmp"].'</option>'";
}
return $output;
}
function fill_Abs($connect)
{
$output = '';
$sql = "SELECT NomEmp,PrénomEmp,DateD,DateF,CauseAbs,justifié
FROM absence.absences
WHERE
MONTH(DateF) = MONTH(NOW())
AND
YEAR(DateF) = YEAR(NOW());";
$result = mysqli_query($connect, $sql);
echo "<table>
<tr>
<th>NomEmp</th>
<th>PrénomEmp</th>
<th>DateD</th>
<th>DateF</th>
<th>CauseAbs</th>
<th>justifié<th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['NomEmp'] . "</td>";
echo "<td>" . $row['PrénomEmp'] . "</td>";
echo "<td>" . $row['DateD'] . "</td>";
echo "<td>" . $row['DateF'] . "</td>";
echo "<td>" . $row['CauseAbs'] . "</td>";
echo "<td>" . $row['justifié'] . "</td>";
echo "</tr>";
}
$output="</table>";
return $output;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Webslesson Tutorial | Multiple Image Upload</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
/>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js">
</script>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js">
</script>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
<script>
$(document).ready(function(){
$('#nom').change(function(){
var NomEmp = $(this).val();
$.ajax({
url:"usr.php",
method:"POST",
data:{NomEmp:NomEmp},
success:function(data){
$('#show_Abs').html(data);
}
});
});
});
</script>
</head>
<body>
<br /><br />
<div class="container">
<h3>
<label for="nom">Selectionnez le nom de l'employé </label>
</br></br>
<select name="nom" id="nom">
<option value="">Tout les employés</option>
<?php echo fill_emp($connect); ?>
</select>
<br /><br /> <br />
<div class="row" id="show_Abs">
<?php echo fill_Abs($connect);?>
</div>
</h3>
</div>
</body>
</html>
这是usr.php:
<?php
//load_data.php
$connect = mysqli_connect("localhost", "root", "ntr-ktb123", "absence");
$output = '';
if(isset($_POST["NomEmp"]))
{
if($_POST["NomEmp"] != '')
{
$sql = "SELECT NomEmp,PrénomEmp,DateD,DateF,CauseAbs,justifié
FROM absence.absences
WHERE
MONTH(DateF) = MONTH(NOW())
AND
YEAR(DateF) = YEAR(NOW()) WHERE NomEmp = '".$_POST["NomEmp"]."'";
}
else
{
$sql = "SELECT NomEmp,PrénomEmp,DateD,DateF,CauseAbs,justifié
FROM absence.absences
WHERE
MONTH(DateF) = MONTH(NOW())
AND
YEAR(DateF) = YEAR(NOW());";
}
$result = mysqli_query($connect, $sql);
$output="<table>
<tr>
<th>NomEmp</th>
<th>PrénomEmp</th>
<th>DateD</th>
<th>DateF</th>
<th>CauseAbs</th>
<th>justifié<th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['NomEmp'] . "</td>";
echo "<td>" . $row['PrénomEmp'] . "</td>";
echo "<td>" . $row['DateD'] . "</td>";
echo "<td>" . $row['DateF'] . "</td>";
echo "<td>" . $row['CauseAbs'] . "</td>";
echo "<td>" . $row['justifié'] . "</td>";
echo "</tr>";
}
echo $output;
}
?>
有人能帮我找出这个问题吗?任何帮助都将不胜感激。谢谢。
2条答案
按热度按时间1l5u6lss1#
只需更改如下所述的ajax调用:
同样在你的html里找不到一个id为show\u abs的标签。所以只要把div修改成
现在我认为php函数
<?php echo fill_Abs($connect);?>
没有必要。当页面第一次加载时,将生成ajax调用,并且在更改select标记时,将生成select标记调用,并传递所选参数以获得适当的结果。igetnqfo2#
我们需要更多的信息来帮助解决这个问题,但我会尽力让事情进展顺利。
我猜是的
$_POST["NomEmp"]
在usr.php中是===“”因此else正在运行,它再次显示整个表。我不知道您在中设置“value”属性是什么
fill_emp
功能。它的价值是什么$row[1]
在那个函数中?