简体   繁体   English

在AJAX请求后重置DOM

[英]Resetting DOM After AJAX Request

I am trying to implement a search functionality powered by an AJAX requests on this site here 我正在尝试在此站点上实现由AJAX请求提供支持的搜索功能

http://polypodhub.com/ISL/ http://polypodhub.com/ISL/

You can notice that when you start typing in the input, the AJAX call starts fetching the file names. 您可能会注意到,当您开始输入输入内容时,AJAX调用便开始获取文件名。 So for instance if you type "S" it will fetch all the file names that have "S" in them. 因此,例如,如果您键入“ S”,它将获取所有带有“ S”的文件名。 Now what I'm having trouble with is what happens after you delete your search query. 现在,我遇到的麻烦是删除搜索查询后会发生什么。 What I would like to happen is for the table under the London category to reappear, but the AJAX request is deleting it. 我想使伦敦类别下的表重新出现,但是AJAX请求正在删除它。

Any idea as to what I should do to reset things when the input field is empty? 关于在输入字段为空时我应该如何进行重置的任何想法?

This is my AJAX request 这是我的AJAX请求

<?php
$section='downloads';
$table="downloads";
$schoolSelectorTable="downloadsinschool";
$schoolSelector="downloads";
$folder="../../downloads/";


    $db_host = "localhost";
    $db_user = "root";
    $db_pass = "";
    $db_name = "isl";


mysql_connect($db_host, $db_user, $db_pass, $db_name);
mysql_select_db("isl") or die(mysql_error());

$partialStates = $_POST['partialState'];
$states = mysql_query("SELECT * FROM `downloads` LEFT JOIN `downloadsinschool` ON `downloads`.`id`=`downloadsinschool`.`downloads` WHERE `school` = 2 AND `title` LIKE '%$partialStates%'" );


if($_POST['partialState']){

while($state = mysql_fetch_array($states)){
    echo "<tr><td class='tlong'>".$state['title']."</td></tr>";
}
}

?>

This is the form which contains the search field: 这是包含搜索字段的表单:

<form style="background-color:;width:;" name="searchField">

<input type="text" name="search" class="downloadSearchBar" onkeyup = "getStatesLondon(this.value), getStatesSizeLondon(this.value), getStatesSurrey(this.value),getStatesSizeSurrey(this.value)" />
</form>

This is the table which is being fed data by the database and then filtered by the AJAX call: 该表由数据库馈送数据,然后由AJAX调用过滤:

    <?php
        //$query = "SELECT * FROM `downloads`, `downloadsinschool` WHERE `school` = '2'";
        $query = "SELECT * FROM `downloads` LEFT JOIN `downloadsinschool` ON `downloads`.`id`=`downloadsinschool`.`downloads`
        WHERE `school` = 2 ORDER BY DAT DESC";
        $result=$connection->query($query);

        echo '<table class="resultsLondon" style="float:left;margin-top:;margin-right:30px;">';
              // echo '<div class="resultsLondon"></div>';
            // echo'<tr><td class="resultsLondon" class="tlong"><a style="padding-left:5px;"></a></td>
            // <td class="resultsSizeLondon" style="width:px;overflow:hidden;"><a style="width:10px;overflow:hidden;" href="#"></a></td></tr>';
        while($row = $result->fetch_array()) {
            echo '<tr><td id="" class="tlong"><a style="padding-left:5px;" href="downloads/'.$row['filename'].'">'.$row['title'].'</a></td>
            <td style="width:px;overflow:hidden;"><a style="width:10px;overflow:hidden;" href="#">'.$row['filesize'].'</a></td></tr>';
          }
        echo '</table>';

    ?>

And this is the AJAX method: 这是AJAX方法:

    function getStatesLondon(value){
    $.post("update/getStatesLondon.php", {partialState:value}, function(data){
      $(".resultsLondon").html(data);
    });
  }

You return 0 if the partialState is empty. 如果partialState为空,则返回0。 You don't want that, just return the full set of results if the search string is empty: 您不需要这样做,如果搜索字符串为空,则只返回完整的结果集:

$partialStates = $_POST['partialState'];
$query = "SELECT * FROM `downloads` LEFT JOIN `downloadsinschool` ON `downloads`.`id`=`downloadsinschool`.`downloads` WHERE `school` = 2";
if ($partialStates) {
    $query += "AND `title` LIKE '%$partialStates%'"
}

$states = mysql_query($query);
while($state = mysql_fetch_array($states)){
    echo "<tr><td class='tlong'>".$state['title']."</td></tr>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM