单选按钮更改无法正常工作ajax

时间:2014-11-13 16:42:37

标签: javascript php jquery ajax

我有一个带有值的单选按钮:"按标题" "演员"。如果用户选择按标题,文本框自动完成显示电影列表在文本框中包含用户类型一词,如果用户选择"按演员"相同的文本框将显示演员列表。此外,如果是"按标题"有一个按钮"通过这个标题搜索电影"这将打开一个显示电影列表的新窗口,其中包含用户在文本框中插入的单词。 (相同的按钮将显示在"由演员",显示该演员的电影列表)

问题:

如果用户选择"按演员",并在文本框中写一个演员姓名,如"汤姆克鲁斯",然后点击按钮"该演员的电影" ;,打开一个新窗口(" movielist.php"),显示电影列表,以及#34; Tom cruise"。现在,如果用户更改单选按钮并选择"按标题"写一个标题名称,如#34; Frozen"然后点击按钮"按此标题播放电影",新窗口打开,但它显示的电影列表是通过" Tom cruise"而不是电影列表包含单词" Frozen"。

此图片可能会更清楚地显示我的问题:

enter image description here

这是代码:

<div id="m_scents" class="field">
   <label style="margin-bottom:10px;" for="m_scnts"></label>
    <p>
     <input class="autofill4" type="textbox" name= "q27[]" id="q" placeholder="Enter movie, actor or director name here" />
     <input type="button" value="Search" id="btnSearch" />
   </p>
 </div>

$(document).ready(function () {
      $("input[id='selectType']").change(function(){
               $("#q").val('');

               if ($(this).val() == "byTitle") {
                    $("#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;
                        }
                    });

                 } else 

                   if ($(this).val() == "byActor"){
                             $("#m_scents").show();
                             $("#q").focus();
                             $("#q").autocomplete({
                                 source: "actorsauto.php",
                                 minLength: 2,
                                 focus: function( event, ui ){
                                      event.preventDefault(); 
                                      return false;
                                 },
                                 select: function (event, ui){ 
                                      window.selectedVal = ui.item.value;
                                 }
                             }); 
  } 

 $('#btnSearch').on('click', function (e) {
           window.textbox = $('#q').val();
           popupCenter("movielist.php","_blank","400","400");
  });

这是movielist.php:

<div id= "field"
</div>
 <script type="text/javascript">

  var textbox = parent.window.opener.textbox;
 $.ajax({
         url: 'childfilm.php',
         datatype: "json",
         //I FEEL THE PROBLEM IS IN THE LINE BELOW WHERE ALL 3 VALUES ARE PASSED IN CASE OF CLICKING ANY BUTTON.. 
         data:{p:textbox}, 
         success: function(response) {     
                     $("#field").html(response);
                   }
        });
</script>*

这里是childfilm.php:

<?php
 if(isset($_GET['q']) && !empty($_GET['q'])){  //IN CASE OF BY ACTOR
  try{
    include('imdbConnection.php');
    $sql = $conn->prepare("SELECT DISTINCT movieName FROM cast_movie WHERE castName = :q");
    $sql->execute(array(':q' => $_GET['q']));

    while($rows = $sql->fetch(PDO::FETCH_ASSOC)){
       $option = '<a href=movie.php?title="' . $rows['movieName'] . '">' . $rows['movieName'] . '</a><br />';
       $html .= $option;

       }

    } catch(PDOException $e){
           echo 'ERROR: ' . $e->getMessage();
       }

   echo $html;       
   exit;
}

?>

<html>
<head>
  <meta http-equiv="content-Type" content="text/html" charset=UTF8" />
  <link type="text/css" rel="stylesheet" href="filmstyle.css" media="screen" />
</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'])) {  // IN CASE OF BY TITLE
                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()) {
                        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 matching your search</td>';
                    }
             }
                ?>
            </table>    
        </div>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

正如我试图解释的那样,你可以这样做。

AJAX:

每次更改研究类型时,都应更新变量searchType。

var searchType = parent.window.opener.searchType; 
var textbox = parent.window.opener.textbox;
 $.ajax({
    url: 'childfilm.php',
    datatype: "json",
    data:{ q:textbox, t:searchType}, 
    success: function(response) {     
        $("#field").html(response);
    }
});

PHP:

<?php
$searchText = (isset($_GET['q'])) ? $_GET['q'] : "";
$searchType = (isset($_GET['t'])) ? $_GET['t'] : ""; /* type of research */

 if($searchText !== "")
 {

  switch ($searchType) 
  {
    case 'byActor':
      $query = "SELECT DISTINCT movieName FROM cast_movie WHERE castName = :q";
      break;

    case 'byTitle':    
    default:
      $query = "SELECT DISTINCT movieName FROM film_info WHERE movieName LIKE :q";
      $searchText = "%".$searchText."%";
      break;
  }

  try
  {
    include('imdbConnection.php');
    $sql = $conn->prepare($query);
    $sql->execute(array(':q' => $searchText));
    $html = "";

    while($rows = $sql->fetch(PDO::FETCH_ASSOC))
    {
      $html .= '<a href=movie.php?title="' . $rows['movieName'] . '">' . $rows['movieName'] . '</a><br />';
    }

  } 
  catch(PDOException $e)
  {
    echo 'ERROR: ' . $e->getMessage();
  }

   echo $html;       
   exit;
}