简体   繁体   中英

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

I created a web-page,where you can search plant's scientific name . Type plant name in search_text it will give you results in search_result(live search like google and facebook search bar) . Ex: when you will type C in search input, in search result you will get C related search. Like C typed in search input, in search result it will start showing Cucumber(Cucumis sativus), Capsicum(Capsicum annuum), etc.

Now I want when you will click on Cucumber(Cucumis sativus) in search result, it have to direct to home.php/Cucumber . Or when user click on Capsicum(Capsicum annuum), it have to direct on home.php/Capsicum .

And on home.php in body tag I want to display plant name with their scientific name. And in para tag information related to plant search result.

index.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>

search.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>';
?>

Please correct this line in index.php

<style type="text/javascript">

with

<script type="text/javascript">

There are many ways of doing this

Passing the name of the plant as a GET param is not an option?

You could do

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

As the response of your server, that would create the link and in home.php you retrieve the plant name with $_GET['plantname'].

in home.php you do

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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