【问题标题】:Search-engine: Php get data from anchor-tag搜索引擎:Php 从锚标签获取数据
【发布时间】:2018-08-29 16:53:12
【问题描述】:

我在这里有这个 html 搜索:

      <form action="index.php" method="POST">
      <input id="search" type="text" placeholder="Search for Friends" name="search_name">
      <input class="submit" type="submit" name="search-submit" value="Search">
      </form>

还有这个搜索引擎的php代码:

<?php
  if (isset($_POST['search-submit'])) {
   $search_name = $_POST['search_name'];
   $aVar = mysqli_connect('localhost','root','','socialnetwork');
   $result = mysqli_query($aVar, "SELECT username FROM users WHERE username LIKE '%$search_name%'");
   $found = 1;
   while ($row = mysqli_fetch_assoc($result)) {
     $username = $row['username'];
     @$output .= '<h2 class="friends-display"><a href="">'.$username.'</a></h2><hr>'; 
   }
 }

 ?>

现在这段代码运行良好。它允许用户搜索其他用户。 锚标记 "friends-display" 使用变量 $output 显示代码的结果。 $output 稍后会在旁边回显。

我的问题如下:我想做一个 if 语句,所以当用户点击锚标签 "friends-display" 时,它应该显示用户拥有的用户名的个人资料点击。

示例:您搜索 Mike 并找到此用户名。然后点击它,它应该会显示 Mike 的个人资料。我怎样才能用锚标签做这个?

我试过if isset(),但对我不起作用。

【问题讨论】:

  • 您好,欢迎来到 SO。我把你的长段剪成多段。我知道你不是以英语为母语的人,但你必须把冗长的解释分开一点,以使它们更具可读性。
  • @Nic3500 非常感谢:)下次我会做的。

标签: php jquery html sql anchor


【解决方案1】:

如下改变输出变量

....
while ($row = mysqli_fetch_assoc($result)) { 
    $username = $row['username'];
     @$output .= '<h2 class="friends-display"><a href="profile.php?user='.$username.'">'.$username.'</a></h2><hr>'; 
}
....

创建一个新文件 profile.php 并添加以下基本行。

<?php
If(isset($_GET['user']) && !empty($_GET['user'])){
    $username = $_GET['user'];
    // check for username found in database. 
    // if not found exit with error "user not found"
    // else show user profile
}else{
    Die("unothrized access");
}

我希望这将指导您实现目标.. 快乐编码:)

【讨论】:

    猜你喜欢
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    相关资源
    最近更新 更多