实时搜索不传递变量

问题描述 投票:0回答:1

我正在进行显示图像缩略图的实时搜索。这很好用。问题是...当我单击缩略图时,它不会将数据从表单输入字段发送到 movie.php 页面。 movie.php 页面需要那些来加载适当的内容。如果有人能提供帮助,我们将不胜感激!

搜索页面

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

<script>
$(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("search_backend.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", function(){
        $(this).parents(".search-box").find('input[type="text"]').val($(this).text());
        $(this).parent(".result").empty();
    });
});
</script>
</head>

<body>
    <div class="search-box">
        <input type="text" autocomplete="off" placeholder="Search for a movie" />
        <div class="result"></div>
    </div>

搜索后端

<?php include "../includes/db_connector.php";
 
if(isset($_REQUEST["term"])){
    
    $search_keyword = $_REQUEST["term"];
    // Prepare a select statement
    $sql = "SELECT title, img FROM jeffbox WHERE title LIKE '%" . $search_keyword . "%'";
    
    if($stmt = mysqli_prepare($db_connect, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "s", $param_term);
        
        // Set parameters
        $param_term = $search_keyword . '%';
        
        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            $result = mysqli_stmt_get_result($stmt);
            
            // Check number of rows in the result set
            if(mysqli_num_rows($result) > 0){
                // Fetch result rows as an associative array
                while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
                    $img = $row["img"];
                    $title = $row["title"];
                    ?>

<form id="form-search" method="post" action="movie.php">
    <input id="btn_top" style="display: none;" type="text" name="title" id="title" value="<?php echo $title ?>" />
    <input id="btn_top" style="display: none;" type="text" name="latest" id="latest" value="<?php echo $title ?>" />
    <input id="new_releases" type="image" name="image" src="images/<?php echo $img ?>">
</form>

<?php
} } else { echo "<p>No movies found</p>"; }
  }
 }
     
// Close statement
mysqli_stmt_close($stmt);
}
 
// close connection
mysqli_close($db_connect);
?>

我试过多次搜索都无济于事...

javascript php html forms search
1个回答
-1
投票

通常你会使用提交按钮来触发表单提交,比如

<input type=submit value="Submit">

但是,如果你想让图片触发表单提交,其中一种方法是使用下面的javascript:

<script>

function submitForm(originator)
{
    var pN=originator.parentNode;
    while (true)
    {
        if (pN&&pN.nodeName=='FORM')
        {
            pN.submit();
            break;
        }
        pN=pN.parentNode;
    }
}

</script>

然后换行:

 <input id="new_releases" type="image" name="image" src="images/<?php echo $img ?>">

<img id="new_releases"  src="images/<?php echo $img ?>" onclick="javascript:submitForm(this);">

另一方面,您提到当您单击图像时,表单中的所有数据字段都被“清除”,只有当您的脚本中某处清空表单数据时才会发生,请修复它,否则目标表单无法从提交的表单中接收数据。

© www.soinside.com 2019 - 2024. All rights reserved.