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


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.hp:

<?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;
}
?>

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

在ajax调用中,您发送一个POST请求,并尝试在php中使用$_GET获取params。

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

将CCD_ 3方法更改为CCD_。

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

将CCD_ 5改变为CCD_。