簡體   English   中英

傳遞一個jQuery變量到PHP代碼

[英]pass a jquery variable to php code

這是我的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,“ SELECT * FROM employee ORDER BY id $ x”);

在你的jQuery代碼

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

在你的PHP代碼中使用

$_POST['sort'] 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM