Javascript 下拉式post实时搜索,添加onclick重定向到post(php页面)

Javascript 下拉式post实时搜索,添加onclick重定向到post(php页面),javascript,php,jquery,Javascript,Php,Jquery,你好, 我正在编写一个主页来学习php和javascript。我决定使用jQuery和php进行livesearch。 它工作得很好,但我想知道如何将onclick函数集成到找到的标题中,该函数将重定向到viewpost.php,从而打开单击的标题并打开文章 索引页上的我的HTML搜索部分: <!-- Search Widget --> <div class="card my-4"> <div class="card bg-succes

你好, 我正在编写一个主页来学习php和javascript。我决定使用jQuery和php进行livesearch。 它工作得很好,但我想知道如何将onclick函数集成到找到的标题中,该函数将重定向到viewpost.php,从而打开单击的标题并打开文章

索引页上的我的HTML搜索部分:

<!-- 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> 
id  title content categorie_id pubdate views short_details  
最后是我的viewpost.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
/* 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);
?>
<?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>';
        ?>

我是否需要通过jQuery获取文章id,并将其发布到viewpost.php? 我非常感谢你的帮助


您需要更改此PHP“backend search.PHP”文件:

此代码用于

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

”; } } 其他的 { echo“未找到匹配项”

”; }
此代码

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>";
}
if($stmt->rowCount()>0)
{
而($row=$stmt->fetch())
{
回声“

”; } } 其他的 { echo“未找到匹配项”

”; }
Hi您可以使用
a
标记,即:

”;
因此当用户单击任何标题时,它将被重定向到带有所需
id
viewpost
页面。工作非常完美!