获取文本框值而不是自动完成值javascript

时间:2014-11-13 11:47:42

标签: javascript php jquery autocomplete

我有一个带有jQuery自动完成功能的文本框。我需要的是:当我点击按钮"电影按照这个标题"时,我希望看到电影列表包含文本框中的术语(即使在自动完成中找不到) ,虽然我只通过自动完成选择看到电影列表。

下图显示了从自动完成列表中选择电影的情况(效果很好): enter image description here

当我只写一些单词时就是这种情况(例如,如果用户忘记了整个名字或者无法在自动完成列表中找到):

enter image description here

这是代码:

<script type="text/javascript">
var selected;

$(document).ready(function () {
      $("input[id='selectType']").change(function(){
               if ($(this).val() == "byTitle") {
                    $("#m_scents2").hide();
                    $("#btnMove").show();
                    $("#m_scents").show();
                    $("#q").focus();
                    $("#q").autocomplete({
                        minLength: 0,
                        delay:5,
                        source: "filmsauto.php",
                        focus: function( event, ui ){
                             event.preventDefault(); 
                             return false;
                        },
                select: function( event, ui ) {
                               window.selected = ui.item.value;
                        }
                    });
  }
$('#btnMove').on('click', function (e) {
           popupCenter("movieBytitle.php","_blank","400","400");
  });

这是movieBytitle.php:

<script type="text/javascript">
 var selected = parent.window.opener.selected; 
 $.ajax({
         url: 'childfilm.php',
         datatype: "json",
         data:{p:selected},
         success: function(response) {      
                     $("#field").html(response);
                   }
        });

</script>

这是childfilm.php:

if(isset($_GET['p']) && !empty($_GET['p'])){
    include('imdbConnection.php');
    $query = $conn->prepare("SELECT DISTINCT movieName, year, posterLink FROM film_info WHERE movieName LIKE :p");
    $query->execute(array(':p' => '%'.$_GET['p'].'%'));
}
?>

<html>
<head>
....
</head>

<body>
 <div class= "movielist"> 
  <table id= "films">
    <tr>
        <th></th>
        <th>year</th>
        <th>Title</th>
    </tr>
    <?php while($row = $query->fetch(PDO::FETCH_ASSOC)): ?>  //THIS IS LINE 53 WHERE THE ERROR IS SHOWN IN THE SECOND IMAGE
    <tr>
        <td><img class='imdbImage' id="image" src='imdbImage.php?url=<?php echo $row['posterLink']; ?>' alt="" /></td>
        <td><label id='year'><?php echo $row['year']; ?> </label></td>

        <td><a href="movie.php?title=<?php echo urlencode($row['movieName']); ?>"><?php echo $row['movieName']; ?></a></td>

    </tr>
    <?php endwhile; ?>
  </table>    
 </div>
</body>
</html>

有人可以告诉我如何获取文本框值而不是自动完成值吗?

更新:最终解决方案

由于问题太长而且最终解决方案是在评论中实现的,我在这里更新了答案:感谢@lolka_bolka,这最终帮助了我:将p的值更改为 - &gt; ($('#q').val())而不是传递选定的自动完成文字.. :))

1 个答案:

答案 0 :(得分:2)

修改

根据OP更新,我们在评论中进行了对话,请阅读。

我认为,如果没有$_GET["p"],您的问题就是,$query将不存在,因此您无法将其用作对象。

<html>
    <head>
        ....
    </head>
    <body>
        <div class= "movielist"> 
            <table id= "films">
                <tr>
                    <th></th>
                    <th>year</th>
                    <th>Title</th>
                </tr>
                <?php
                if (isset($_GET['p']) && !empty($_GET['p'])) {
                    include('imdbConnection.php');
                    $query = $conn->prepare("SELECT DISTINCT movieName, year, posterLink FROM film_info WHERE movieName LIKE :p");
                    $query->execute(array(':p' => '%' . $_GET['p'] . '%'));
                    if ($query->rowCount()) {
                        //If there are no $_GET["p"] then there wont be $query,
                        //So you can not fetch it!!!
                        while ($row = $query->fetch(PDO::FETCH_ASSOC)):
                            ?> 
                            <tr>
                                <td><img class='imdbImage' id="image" src='imdbImage.php?url=<?php echo $row['posterLink']; ?>' alt="" /></td>
                                <td><label id='year'><?php echo $row['year']; ?> </label></td>

                                <td><a href="movie.php?title=<?php echo urlencode($row['movieName']); ?>"><?php echo $row['movieName']; ?></a></td>

                            </tr>
                            <?php
                        endwhile;
                    } else {
                        echo '<td colspan="3">Sorry, there are no film like this</td>';
                    }
                }
                ?>
            </table>    
        </div>
    </body>
</html>