-1

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>';
?>
revengeance
  • 851
  • 7
  • 21
Jhon
  • 29
  • 8
  • 1
    Your question title talks about sessions, but I don’t see you using them anywhere here. And I don’t see how using them would make sense either, since you apparently want to get information that was send with the _current_ request. If your _actual_ question is how to get the value `Cucumber`, when `home.php/Cucumber` was requested, the answer is called PATH_INFO. – misorude Jan 16 '19 at 10:38
  • I'm voting to close since it's unclear what you are asking, no effort to format code readable. – Pinke Helga Jan 16 '19 at 10:44
  • And how to print Cucumber and their scientific on home.php – Jhon Jan 16 '19 at 10:45
  • OT: widely open to sql injection : https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 – Eakethet Jan 16 '19 at 10:48
  • I have rejected the edit because it does harm to the snipet by changing tags. – mickmackusa Jan 16 '19 at 10:56

2 Answers2

0

Please correct this line in index.php

<style type="text/javascript">

with

<script type="text/javascript">
0

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'];
}