Hi, i am coding a homepage to learn php and javascript. I decided to use a livesearch using jQuery and php. It is working well ,but i wonder how i can integrate to the found titles an onclick function that will redirect to the viewpost.php so it opens the clicked title and opens the post.

My HTML search part on index page:

<!-- Search Widget -->
<div class="card my-4">
<div class="card bg-success">
  <h5 class="card-header">Search</h5>
  <div class="card-body">
        <div class="search-box">
<input type="text" autocomplete="off" placeholder="Search country..." />
<div class="result"></div>
</div>
  </div>
</div>
</div> 

jQuery part for livesearch that redirect to php page(backend-search.php)

<script type="text/javascript">
$(document).ready(function(){
    $('.search-box input[type="text"]').on("keyup input", function(){
        /* Get input value on change */
        var inputVal = $(this).val();
        var resultDropdown = $(this).siblings(".result");
        if(inputVal.length){
            $.get("backend-search.php", {term: inputVal}).done(function(data){
                // Display the returned data in browser
                resultDropdown.html(data);
                
            });
        } else{
            resultDropdown.empty();
        }
    });
    
    // Set search input value on click of result item
    $(document).on("click", ".result p", function(){
        $(this).parents(".search-box").find('input[type="text"]').val($(this).text());
        $(this).parent(".result").empty();
    });
});
</script>

PHP backend-search.php

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
require_once "pdoconfig.php";
 
// Attempt search query execution
try{
    if(isset($_REQUEST["term"])){
        // create prepared statement
        $sql = "SELECT * FROM articles WHERE title LIKE :term";
        $stmt = $db->prepare($sql);
        $term = $_REQUEST["term"] . '%';
        // bind parameters to statement
        $stmt->bindParam(":term", $term);
        // execute the prepared statement
        $stmt->execute();
        if($stmt->rowCount() > 0){
            while($row = $stmt->fetch()){
                echo "<p>" . $row["title"] . "</p>";                
            }
        } else{
            echo "<p>No matches found</p>";
        }
    }  
} catch(PDOException $e){
    die("ERROR: Could not able to execute $sql. " . $e->getMessage());
}
 
// Close statement
unset($stmt);
 
// Close connection
unset($db);
?>

That is my table structure called articles:

id  title content categorie_id pubdate views short_details  

And finally my viewpost.php

<?php
 $stmt = $db->prepare('SELECT id, title, text, pubdate FROM articles WHERE id = :id');
 $stmt->execute(array(':id' => $_GET['id']));
 $row = $stmt->fetch();
//if post does not exists redirect user.
if($row['id'] == ''){
    header('Location: ./');
    exit;
}
            echo "<br>";
            echo "<div class='card mb-4'>" . "<div class='card-body'>";
            echo "<h2 class='card-title'>";     
            echo $row['title'] . "</h2>";
            echo "<div class='card-footer text-muted'>";
            echo $row['pubdate'];
            echo "</h2>";
            echo "<p class='card-text'>";
            echo $row['text'];
            echo "</p>";
            echo '</div>';
        ?>

Do i need to get the articles id with the jQuery and somehow post it onclick to viewpost.php ? I do appreciate all help ..

enter image description here

Answer

You Need To Change This PHP “backend-search.php” File :

This Code To

if($stmt->rowCount() > 0)
{
   while($row = $stmt->fetch())
   {
      echo "<p>" . $row["title"] . "</p>";                
   }
} 
else
{
    echo "<p>No matches found</p>";
}

This Code

if($stmt->rowCount() > 0)
{
   while($row = $stmt->fetch())
   {
      echo "<p><a href="viewpost.php?id=".$row["id"]."">". $row["title"] . "</a></p>";                
   }
} 
else
{
    echo "<p>No matches found</p>";
}