单击按钮时清除查询结果

时间:2015-04-27 09:14:02

标签: php html

//html form
<p>Search Name of Applicants</p>
<form name ="searchForm" method="post" action="admin.php">
    <input name="searchIto" type="text" size ="40" maxlength="50"/>
    <input type="submit" name="searchMo" value="Search"/>
    <input type="reset" name="resetMo" value="Clear"/>
</form>
//database connection
<?php
    $user_name = "root";
    $password = "";
    $database = "applications";
    $server = "127.0.0.1";
    $db_handle = @mysql_connect($server, $user_name, $password);
    $db_found = @mysql_select_db($database, $db_handle);
?>
//query
<?php
    if(!isset($_POST['searchIto'])){
        header('Location:admin.php');
    }
    $search_sql="SELECT * FROM loanapplications WHERE firstName LIKE '%".$_POST['searchIto']."%' OR lastName LIKE '%".$_POST['searchIto']."%'";
    $search_query=mysql_query($search_sql);
    if(mysql_num_rows($search_query)!=null){
        $search_rs=mysql_fetch_assoc($search_query);
    }
?>
<p>Search Results</p>
<?php
    if(mysql_num_rows($search_query)!=null){
        do {
?>
<span>ID:&nbsp;</span><?php echo $search_rs['loantype'];?><br>
<span>First Name:&nbsp;</span><?php echo $search_rs['firstName'];?><br>
<span>Last Name:&nbsp;</span><?php echo $search_rs['lastName'];?><br>
<?php
        } while($search_rs=mysql_fetch_assoc($search_query));
    } else {
        echo "No results found";
    }
?>

如果我点击清除按钮,我想清除查询结果,如果我刷新页面,查询仍然存在,如果我刷新或点击或离开管理页面,查询将被清除。

2 个答案:

答案 0 :(得分:0)

在代码结束时写下面的代码 -

<?php 
 mysql_free_result($search_query);
?>

答案 1 :(得分:0)

您需要在代码中进行一些更改

首先,Html将结果div添加为搜索结果的包装,并带有清除按钮。

<button id="clear" value="clear" ></button>
    <div id="result">
        <p>Search Results</p>
        <?php
        if(mysql_num_rows($search_query)!=null){
        do{?>
        <span>ID:&nbsp;</span><?php echo $search_rs['loantype'];?><br>
        <span>First Name:&nbsp;</span><?php echo $search_rs['firstName'];?><br>
        <span>Last Name:&nbsp;</span><?php echo $search_rs['lastName'];?><br>
        <?php
        }while($search_rs=mysql_fetch_assoc($search_query));
        } 
        else{
        echo "No results found";
        }
        ?>
    </div>

现在添加一些用于清除搜索结果的脚本

   <script>
        $("#clear").click(function (event) {
            $("#result").html(" <p>Search Results</p>"."No results found");

        });
    </script>

因此您需要刷新页面,您将成功清除搜索结果。