0

誰か助けてください、私は本当に頭がおかしくなりそうです!.

私はいくつかの質問を含む PHP フォームを持っていますが、その質問の 1 つは「あなたのお気に入りの映画はどれですか?」です。そのために、正常に機能するjQueryオートコンプリート機能を使用しました!. ただし、ユーザーが映画の名前を忘れても、その映画に出演した俳優は覚えている可能性があります。そこで、ユーザーがオートコンプリート テキスト ボックスに俳優/女優の名前 (「トム クルーズ」など) を入力できるようにし、挿入された俳優の名前に基づいて、俳優が出演した映画のリストを含む動的なドロップダウン メニューを追加する必要があります。 (例:トム・クルーズ)が出演しています。

これは私が試したものですが、うまくいきません:((

<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になるのはなぜですか?

4

1 に答える 1

0

ajax 呼び出しでPOSTリクエストを送信し、 $_GETphp でパラメーターを取得しようとします。

$.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$_POSTます。

于 2014-10-22T15:24:41.353 に答える