doushoubu5360 2014-11-13 16:42
浏览 37
已采纳

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

I have radio button with values: "By title" and "By actor". If user select By title the textbox auto-complete shows list of movies contain the word user type in the textbox, and if user select "By actor" the same textbox will show list of actors. Also, in case of "by title" there is a button "search movies by this title" which will open a new window showing list of movies contain the word that user inserted in the textbox. (The same button will be shown in case of "by actor", which shows list of movies by that actor)

Problem:

If user select "by actor", and write an actor name in the textbox like "Tom Cruise", and then click the button "movies by this actor", a new window ("movielist.php") is opened showing list of movies by "Tom cruise". Now if then user change the radio button and select "by title" and write a title name like "Frozen" and click the button "movies by this title", the new window is opened but it shows list of movies by "Tom cruise" instead of list of movies contain word "Frozen".

This image may show my problem more clearly:

enter image description here

This is the code:

<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");
  });

this is 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>*

and here is 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条回答 默认 最新

  • douzhang5295 2014-11-13 19:06
    关注

    As i tried to explain, you could do something like that.

    AJAX:

    the variable searchType should be update each time you change the research type.

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器