繁体   English   中英

根据文本框中插入的值填充动态下拉列表

[英]Populate a dynamic dropdown based on value inserted in the text-box

请有人帮助,我真的要疯了!

我有一个PHP表格,其中包含一些问题,问题之一是“您最喜欢哪部电影?” 为此,我使用了jQuery自动完成功能,效果很好! 但是,用户可能会忘记电影的名称,但会记住在该电影中播放的演员。 因此,我想允许用户在自动完成的文本框中键入演员/女演员名称(例如“ Tom Cruise”),并基于插入的演员名称,添加一个动态下拉菜单,其中包含该演员的电影列表(例如,汤姆·克鲁斯(Tom Cruise))参与其中。

这是我尝试过的,但是没有用:((

<html>
<?php
print_r($_POST);
?> 
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css"/>
</head>

<body>

<input type="textbox" name= "tag" id="tags">
<select id="movieImdbId" name="movieImdbId[]" multiple="multiple" width="200px" size="10px" style=display:none;>
</select>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" />

<script type="text/javascript">
$(document).ready(function () {
                      $("#tags").autocomplete({
                        source: "actorsauto.php",  //php file which fetch actors name from DB
                        minLength: 2,
                        select: function (event, ui){
                        var selectedVal = $(this).val(); //this will be your selected value from autocomplete
                 // Here goes your ajax call.      
                       $.post("actions.php", {q: selectedVal}, function (response){
                // response variable above will contain the option tags.            
                       $("#movieImdbId").html(response).show();
              });
           }
          });     
       });                     
</script>    
</body>
</html>

这是actions.php:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

if(isset($_GET['q']) && !empty($_GET['q'])){
 $q = $_GET['q'];

include('Connection.php');  //connection to the DB
$sql = $conn->prepare("SELECT DISTINCT movieImdbId FROM movie_roleNames WHERE castName = :q");
$sql->execute(array(':q' => $q));
$html = "";

while($row = $sql->fetch(PDO::FETCH_OBJ)){
  $option = '<option value="' . $row->movieImdbId . '">' . $row->movieImdbId . '</option>';
  $html .= $option;
}
echo $html; // <-- this $html will end up receiving inside that `response` variable in the `$.post` ajax call.
exit;
}
?>

问题:为什么当用户在文本框中插入演员名称时,会填充下拉菜单,但会显示EMPTY

在您的ajax调用中,您发送一个POST请求,然后尝试在PHP中使用$_GET获取参数。

$.post("actions.php", {q: selectedVal}, function (response){
    // response variable above will contain the option tags.            
   $("#movieImdbId").html(response).show();
});

$.post方法更改为$.get

要么

if(isset($_GET['q']) && !empty($_GET['q'])){
    $q = $_GET['q'];
}

$_GET更改$_GET $_POST

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM