将jquery变量传递给php代码

时间:2017-09-24 13:38:48

标签: php jquery html

这是我的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像这样: result = mysqli_query($ conn,&#34; SELECT * FROM employee ORDER BY id $ x&#34;);

1 个答案:

答案 0 :(得分:0)

在你的jquery代码中

$.post( "getEmployee.php", { sort: "asc" } );

使用

在您的PHP代码中
$_POST['sort']