如何传递变量到PHP的Ajax处理程序脚本?变量、脚本、程序、Ajax

2023-09-10 14:47:18 作者:那就把他绿了

我有一个小的ajax PHP应用程序,它从一个MySQL数据库导出数据到一个表。该行是链接,点击后会调用一个AJAX功能,这反过来将调用另一个php文件,其中显示从一层相同的数据库不同的查询,而无需重新加载页面。

I have a small ajax php application, which outputs data from a mysql db into a table. The rows are links, which when clicked will call an ajax function, which in turn will call another php file, which displays a different query from the same database in a layer without reloading the page.

我想知道如何同步两个PHP文件的查询。所以,当我点击一个行的基础上,该层将扩大到包括更多的信息,或甚至整个查询。

I would like to know how to synchronize queries between both php files. So when I click on a row in the base page, the layer will be expanded to include additional information, or indeed the whole query.

我想我可以通过在该表格的第一个查询的主键做到这一点,但是我不希望它显示出来,并想知道如果有一个更好的办法来呢?

I was thinking I could do this by having the primary key in the first query for the table, however I don't want it displayed and was wondering if there was a better approach to this?

推荐答案

与jQuery它很简单,我肯定会推荐在Ajax调用使用它,等等。比方说,你有这样的一个表;

with jQuery it's very simple, and I would definitely recommend using it in ajax calls and etc. Let's say you have a table like this;

<table>
 <?php
 // I'm using mysqli class by the way.
 $ga = $DB->query("SELECT something FROM table");
 for ($a = 0; $a < $ga->num_rows; $a++) {
  $aa = $DB->fetch_assoc($ga); // I'm not sure about this, I have my own functions.
  echo "
  <tr class="clickable" id="<?=$aa["Id"] ?>">
   <td>".$aa["NameOfColumn"]."</td>
  </tr>
  ";
 }
 ?>
</table>

和JavaScript的一部分;

and for the javascript part;

<script type="text/javascript">
$(document).ready(function() {
 $(".clickable").on("click", function() {
  // Get our row Id from the rows "id" attribute.
  $id = $(this).attr("id");
  alert($id);
 });
</script>

而不是显示一个警告,你必须改变你需要做的

。首先我会建议使用preloaded格,并改变它的内容,而使用它喜欢;

Instead of displaying an alert you have to change what you need to do. For starters I would recommend using a preloaded div, and changing its content while using it like;

<div id="displayData" style="display: none;">&nbsp;</div>

和JS的功能,你可以用它喜欢;

and for the JS function you can use it like;

$("#displayData").html($id).css("display","block");

的例子有很多,你应该找到最适合你的。

The examples are numerous, and you should find what suits you best.