1

我制作了一个网站,用户可以在其中从下拉菜单中选择一个值并从数据库中获取一些信息。我使用 ajax 将请求发送到数据库(因此当我发送请求时页面不会刷新)。这是jquery函数的一部分:

      $.ajax({
      type:'POST',
      url:'activities_code.php',
      data: {datastr:datastr, datastr1:datastr1},
      success:function(response){

        $("#msg").html(response);                           
            }});}); // there are other functions before..

结果显示在网页的主容器上。它们由标题和一些文本组成。我以这种方式呼应标题,因此它是一个链接。我还给每个元素一个 id 和一个类,以便我以后可以调用它。下面是对应的代码:

    echo "<table id=\"container\">";
$num_results = 0;
while ($row=mysqli_fetch_array($result, MYSQLI_ASSOC)) {
     // Here the columns of title and information are printed
     echo "<tr><td>";
     echo "<a href='test.php' name=\"fd\" id=\"t".$t."\" target=\"_new\"  class='pickanchor' onClick=\"test()\">".$row['title']."</a>";
     echo "<br>";
     echo $row['PK'];
     echo "</td></tr>";

     echo "<tr><td>";
     echo $row['Information'];
     echo "</td></tr>"; 
 }

我现在要做的是:当我单击标题(这是一个链接)时,会打开一个新页面,其中一个 php 脚本运行查询并显示更多信息:这是我所拥有的:

     <?php
     include('connect.php');


    $query = "SELECT title,Information from activities where title='?????'";

$result = mysqli_query($dbcon, $query) or die('no available data');
echo "<table>";
$num_results = 0;
while ($row=mysqli_fetch_array($result, MYSQLI_ASSOC)) {
     // Here the columns of title and information are printed
     echo "<tr><td>";
     echo "<a href='test.php' id=\"t\".$t target=\"_new\">".$row['title']."    </a>";
     echo "</td></tr>";

     echo "<tr><td>";

     echo $row['Information'];

     echo "</td></tr>";
    // Here I sum up the number of the results
     $num_results=$num_results+1;        
 }
     ?>

我正在尝试找到一种方法将我的查询放入 where 子句中,我选择的标题名称:

     $query = "SELECT title,Information from activities where title='?????'";

任何帮助将非常感激。让我知道是否一切都清楚,或者我没有清楚地解释某些观点。谢谢。D.

4

1 回答 1

1

您可以使用全局变量和 URL 参数获取标题。$_GET尝试更改此行:

echo "<a href='test.php' name=\"fd\" id=\"t".$t."\" target=\"_new\"  class='pickanchor' onClick=\"test()\">".$row['title']."</a>";

echo "<a href='test.php?title={$row['title']}' name=\"fd\" id=\"t".$t."\" target=\"_new\"  class='pickanchor' onClick=\"test()\">".$row['title']."</a>";

那么您可以使用以下代码获取标题:

$title = $_GET['title'];

确保您首先对值进行消毒。我希望这能帮到您。

关联:

PHP $_GET

于 2012-10-11T19:18:04.240 回答