使用 jquery 控制 php 页面上数据库结果的限制



无论如何可以使用jquery控制数据库中的条目限制。


我有PHP Data.php

mysqli_query($con,"(select * from social order by id desc limit 30) order by id asc");

但是我希望能够编辑limit 30并使用$.get()在 jquery 中调用单击函数时再次请求 php。这可能吗?


$.get()

 $.get("Data.php", function (data3) {
     $(".data").html(data3);
 });

我的代码现在如下所示:

Data.php

$limit_amt = mysqli_real_escape_string($_GET['limit']);
$result = mysqli_query($con,"(select * from social order by id desc limit ".$limit_amt.") order by id asc");
while($row = mysqli_fetch_array($result))
  {
      echo "<tr class='border_bottom'>";
      echo "<th scope='col'>";
      echo "<div align='left'><span class='name'>".$row['name']."</span></div><br />";
      echo "<span class='text'>".$row['comunicate']."</span><br />";
      echo "<div align='right'><span class='time'>".$row['date']."</span></div>";
      echo "</th>";
      echo "</tr>";
  }

Index.php

$.get("Data.php",{ limit: 40}, function (data3) { .
         $(".data").html(data3);
    });

在你的$.get()

//#btn would represent an HTML element like <input type="button" id="btn">
$("#btn").click(function(e){
    // I have given a static value here but you can change it to a variable or 
    // however you want to pass
    $.get("Data.php",{ limit: 40}, function (data3) { .
         $(".data").html(data3);
    });
}

在您的Data.php

//Fetching the value and saving into a variable
$limit_amt = mysqli_real_escape_string($_GET['limit']);
//Using the fetched value to change the limit amount
mysqli_query($con,"(select * from social order by id desc limit ".$limit_amt.") order by id asc");

我认为您使用的查询中存在一些错误

请尝试

$result = mysqli_query($con,"(select * from social order by id desc limit ".$limit_amt.")");

而不是

$result = mysqli_query($con,"(select * from social order by id desc limit ".$limit_amt.") order by id asc");

最新更新