【发布时间】:2018-03-05 13:32:18
【问题描述】:
这是我的 php 代码
require 'functions/connection.php';
$conn = Connect();
$result = mysqli_query($conn,"SELECT * FROM employee ORDER BY id asc");
echo "
<tr>
<th>Id</th>
<th>First name</th>
<th>Last name</th>
<th>Salary</th>
<th>Start Date</th>
<th>Department</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['salary'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['department'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
这是我的 jquery 代码
<script>
$('.sort').click(function(){
var value = $(this).attr('data-val');
var field_name = $(this).attr('data-field');
$.post( "getEmployee.php", {value:value, field_name:field_name}, function( data ) {
$('.responstable').html(data);
});
if(value=="asc"){
$x="asc";
}
else{
$x="desc";
}
});
</script>
我想将变量 x 从 jquery 代码传递给 php,这样我就可以像这样订购我的数据库 asc 或 desc: 结果 = mysqli_query($conn,"SELECT * FROM employee ORDER BY id $x");
【问题讨论】:
-
您可以将
if-statement 移到您的$.post上方,并将$x添加到您要发布到服务器的对象中。