【问题标题】:pass a jquery variable to php code将 jquery 变量传递给 php 代码
【发布时间】: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 添加到您要发布到服务器的对象中。

标签: php jquery html


【解决方案1】:

在你的 jquery 代码中

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

在你的 php 代码中使用

$_POST['sort'] 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    相关资源
    最近更新 更多