繁体   English   中英

在 PHP 中有什么方法可以在会话中存储 $variable 的 $variable

[英]Is there any way in PHP to store $variable of $variable in session

我创建了一个网页,您可以在其中搜索植物的学名 search_text 中输入植物名称,它会在search_result(实时搜索,如 google 和 facebook 搜索栏)中为您提供结果。 例如:当您在搜索输入中键入 C 时,在搜索结果中您将得到与 C 相关的搜索。 就像在搜索输入中输入 C 一样,在搜索结果中它会开始显示 Cucumber(Cucumis sativus)、Capsicum(Capsicum annuum) 等。

现在我想当你在搜索结果中点击 Cucumber(Cucumis sativus) 时,它必须指向home.php/Cucumber 或者当用户点击 Capsicum(Capsicum annuum) 时,它必须指向home.php/Capsicum

body 标签中的home.php 上,我想用它们的学名显示植物名称。 并在与植物搜索结果相关的para 标签信息中。

索引.php

<!DOCTYPE html>
<html>
   <head>
      <title>Search</title>
      <style type="text/javascript" 
         src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </style>
      <style type="text/javascript">
      function searchq() {
      var searchTxt = $("input[name='search']").val();
      $.get("search.php", {searchVal: searchTxt}, function(output) {
      $("#output").html(output);
      });
      }
      </script>
   </head>
   <body>
      <form action="index.php" method="post">
         <input type="text"  name="search" id="myInput1" autocomplete="off" 
            placeholder="Search username..." onkeydown="searchq(); " />
         <input type="submit" value=">>"/>
      </form>
      <br> 
      <div id="output"></div>
   </body>
</html>

搜索.php

<?php
$con = mysqli_connect('localhost', 'root');
mysqli_select_db($con, 'plant');
$output = '';
if (isset($_GET['searchVal'])) {
    $searchq = $_GET['searchVal'];
    $sql = "select * from type where plant like '%$searchq%'";
    $query = mysqli_query($con, $sql) or die("could not search");
    $count = mysqli_num_rows($query);
    if ($count == 0) {
        $output = 'There is no serach result';
    } else {
        while ($row = mysqli_fetch_array($query)) {
            $plantname = $row['plant'];
            $sciencename = $row['species'];
            $id = $row['id'];
            $output .= '<div>' . $plantname . ' ' . $sciencename . '</div>';
        }
    }
}
echo '<a herf="home.php/">' . $output . '</a></div>';
?>

请更正 index.php 中的这一行

<style type="text/javascript">

<script type="text/javascript">

有很多方法可以做到这一点

将植物名称作为 GET 参数传递不是一种选择?

你可以做

echo "<a href='home.php?plantname={$plantname}' target='_blank'>{$output}</a></div>";

作为服务器的响应,这将创建链接,并在 home.php 中使用 $_GET['plantname'] 检索植物名称。

在 home.php 你做

if(isset($_GET['plantname'])){
    echo $_GET['plantname'];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM