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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!