html Live Search不传递变量

8tntrjer  于 2023-04-04  发布在  其他
关注(0)|答案(1)|浏览(106)

我正在开发一个实时搜索,它可以显示图像缩略图。这很好用。问题是...当我点击缩略图时,它不会将表单输入字段中的数据发送到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);
?>

我试过多次搜索都没有结果。。

8dtrkrch

8dtrkrch1#

通常,您将使用一个提交按钮来触发表单提交,类似于

<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);">

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

相关问题