如何使用 PHP 获取已点击 Link 的文本值

How to get the Text Value of a Clicked Link using PHP

我知道使用 jQuery 获取 link 的文本值非常容易。 但是仅使用 PHP 是不是可以获取文本值?请看看我的代码:

echo '<table>'
foreach($array['data']['results']['titles'] as $data) { 

   $title = $data['title'];
   $id= $data['id'];
   $url = $data['url'];

   echo '<tr>';
   echo '<td>' . '<a href="movie.php" target="_blank">'.$title.'</a>' . '</td>';
   echo '<td>' . '<a href= ' .$url . ' target="_blank" >IMDb Link</a>' . '</td>';
   echo '</tr>'; 
}    

echo '</table>';

This picture shows the output of my code

假设用户点击了第三部电影 - 蝙蝠侠:动画系列。如何使我的 movie.php 页面看起来像这样 --(下图)

The Link Text (which is $title) should be passed to the movie.php page and also the IMDb Link of the corresponding movie (which is stored in the $url variable)

我知道的唯一方法是使用 $_SESSION,但在这种情况下它不起作用,因为它只会存储和传递 foreach 循环的最后一个值

请在这方面帮助我。谢谢:)

尝试使用正确的主键发送所有信息,或者这里是运行时解决方案

echo '<td>' . '<a href="movie.php?name='.$title.'&imdblink='.$url.'" target="_blank">'.$title.'</a>' . '</td>';

不要忘记对变量进行 urlencode

每个 link 应该包含唯一的文件,比如 $id 所以你的 url 应该是 echo '<td>' . '<a href="movie.php?id='.$id.'" target="_blank">'.$title.'</a>' . '</td>'; 您可以传递 $id 的编码值,因此最终用户无法猜测 value.In movie.php 您解码 $id 并在 $id[= 的基础上显示信息15=]

考虑将 $id 作为数据库中特定电影的行 ID。你可以在这里使用 url 编码。您的代码必须是

echo '<td>' . '<a href="movie.php?id='. urlencode($id) . '" target="_blank">'.$title.'</a>' . '</td>';

点击后您的 URL 看起来像。

movie.php?id=3

在你的 movie.php 文件上使用 $id = urldecode($_GET['id']); 获取电影 ID可以再次从数据库中获取相关数据。

如果您有任何问题,请告诉我。