ajax原生 搜索 分页

分页 

<?php 

$link1=mysqli_connect('www.wangjianhong.com','root','root','two_month');
$bookname=isset($_GET['name'])?$_GET['name']:'';
mysqli_query($link1,'set names tf8');
//求出总条数
if ( $bookname ) {
	$sql1="select * from book where bookName like '%$bookname%'";	
} else {
	$sql1="select * from book ";
}

$count=mysqli_query($link1,$sql1);
$count=mysqli_num_rows($count);

//当前在第几页
$page=isset($_GET['page'])?$_GET['page']:1;
//每页显示的条数
$limit=7;
//总长度除以每页显示的条数
$numPage = ceil($count/$limit);
//页数一定比总页数
if ( $page>$numPage ) {
    $page = $numPage;
}
//判断页数不能为0
if ($page<=0) {
	$page = 1;
}
//计算页面偏移量
$x=($page-1)*$limit;

	$link=mysqli_connect('www.wangjianhong.com','root','root','two_month');
	mysqli_query($link,'set names utf8');
	if ( $bookname ) {
	$sql="select * from book where bookName like '%$bookname%' limit $x,$limit";	
} else {
	$sql="select * from book limit $x,$limit ";
}

	$res=mysqli_query($link,$sql);
	while ($arr=mysqli_fetch_assoc($res)) {
		$data[]=$arr;
	}
 ?>
书名搜索<input type="text" id='sel'><button onclick="sel()">搜索</button>
<div id="div">
    <table border="1">
        <h2>图书表</h2>
        <tr>
            <th>选择</th>
            <th>编号</th>
            <th>书名</th>
            <th>作者</th>
            <th>出版社</th>
            <th>出版年月</th>
            <th>图片</th>
            <th>操作</th>
        </tr>

        <?php
        foreach ($data as $key => $value) {
            ?>
            <tr id='<?php echo $value['book_id'];?>'>
                <td><input type="checkbox" name="checkall" value="<?php echo $value['book_id'];?>"></td>
                <td> <?php echo $value['book_id'];?></td>
                <td> <?php echo $value['bookName'];?></td>
                <td> <?php echo $value['bookWriter'];?></td>
                <td> <?php echo $value['bookPress'];?></td>
                <td> <?php echo $value['bookYear'];?></td>
                <td> <img src="./load/<?php echo $value['book_image'];?>" width="50px" height="50px"></td>
                <td>
                    <a href="table.php">返回首页</a>
                    <a href="delete.php?id=<?php echo $value['book_id'];?>">删除</a>
                    <a href="default.php?id=<?php echo $value['book_id'];?>">修改</a>
                    <a href="download.php?path=./load/<?php echo $value['book_image'];?>">下载此图片</a>
                </td>
            </tr>
            <?php
        }
        ?>
    </table>
</div>

//默认第一页
 <input type="hidden" id='page' value="1">

<button onclick="fun1()">全选</button>
<button onclick="fun2()">取消</button>
<button onclick="fun3()">反选</button>
<button onclick="fun4()">批量删除</button>
<button onclick="fun5()">ajax批量删除</button>
<button onclick="fun6()">上一页</button>
<button onclick="fun7()">下一页</button>
<script type="text/javascript">
	function fun6() {
	    var page = document.getElementById('page').value;
        page = page-1;
	    if ( page<1 )
	    {
	        page = 1;
        }
		var ajax= new XMLHttpRequest();
		ajax.open('get', 'sel.php?page='+page,true);
		ajax.send();
		ajax.onreadystatechange=function() {
		    if ( ajax.readyState==4 && ajax.status==200 ) {
		        document.getElementById('page').value=page;
		        div.innerHTML=ajax.responseText;//替换
            }
		}
	}
	function fun7() {
        var page = document.getElementById('page').value;
        page = parseInt(page)+1;
        var ajax= new XMLHttpRequest();
        ajax.open('get', 'sel.php?page='+page,true);
        ajax.send();
        ajax.onreadystatechange=function() {
            if ( ajax.readyState==4&& ajax.status==200 ) {
                document.getElementById('page').value=page;
                div.innerHTML=ajax.responseText;//替换
            }

        }
    }
</script>

<a href="show.php?page=<?php echo $page-1;?>&name=<?php echo $bookname;?>">上一页</a>
<a href="show.php?page=<?php echo $page+1;?>&name=<?php echo $bookname;?>">下一页</a>
<script>
	function fun1(){
		var checkall=document.getElementsByName('checkall');
		for (var i = 0; i < checkall.length; i++) {
			checkall[i].checked=true;
		}
	}
		function fun2(){
		var checkall=document.getElementsByName('checkall');
		for (var i = 0; i < checkall.length; i++) {
			checkall[i].checked=false;
		}
	}
		function fun3(){
		var checkall=document.getElementsByName('checkall');
		for (var i = 0; i < checkall.length; i++) {
			if (checkall[i].checked==false) {
				checkall[i].checked=true;
			}
			else
			{
				checkall[i].checked=false;
			}
		}
	}
		function fun4(){
		var checkall=document.getElementsByName('checkall');
		var str='';
		for (var i = 0; i < checkall.length; i++) {
			if (checkall[i].checked) {
				str=str+checkall[i].value+',';
			}

		}
		var str=str.substr(0,str.length-1);
		location.href='delete.php?id='+string;
	}
	function fun5(){
		var boxs=document.getElementsByName('checkall');
		var str='';
		for (var i = 0; i < boxs.length; i++) {
			if ( boxs[i].checked ) {
				str+=boxs[i].value+',';
			}
		}
		str=str.substr(0,str.length-1)
		var ajax=new XMLHttpRequest();
		ajax.open('get', 'delete.php?id='+str,true);
		ajax.send();
		ajax.onreadystatechange=function(){
			if ( ajax.readyState==4 && ajax.status==200 ) {
				if ( ajax.responseText == true) {
					str=str.split(',');
					for (var i = 0; i < str.length; i++) {
						var tr=document.getElementById(str[i]);
						tr.parentNode.removeChild(tr);
					};

				}

			}
		}

	}

</script>
<script>
	function sel(){
		var userName = document.getElementById('sel').value;
		location.href='show.php?name='+userName;

	}
</script>

搜索

<?php
$link1=mysqli_connect('www.wangjianhong.com','root','root','two_month');
$bookname=isset($_GET['name'])?$_GET['name']:'';
mysqli_query($link1,'set names tf8');

$sql1="select * from book ";


$count=mysqli_query($link1,$sql1);
$count=mysqli_num_rows($count);

//当前在第几页
$page=isset($_GET['page'])?$_GET['page']:1;
//每页显示的条数
$limit=7;
//总长度除以每页显示的条数
$numPage = ceil($count/$limit);
//页数一定比总页数
if ( $page>$numPage ) {
    $page = $numPage;
}
//判断页数不能为0
if ($page<=0) {
	$page = 1;
}
//计算页面偏移量
$x=($page-1)*$limit;
$link=mysqli_connect('www.wangjianhong.com','root','root','two_month');
mysqli_query($link,'set names utf8');
if ( $bookname ) {
$sql="select * from book where bookName like '%$bookname%' limit $x,$limit";
} else {
	$sql="select * from book limit $x,$limit ";
}
$res=mysqli_query($link,$sql);
while ($arr=mysqli_fetch_assoc($res)) {
	$data[]=$arr;
}
?>




$str = "<table border='1'>
    <tr>
        <th>选择</th>
 		<th>编号</th>
 		<th>书名</th>
 		<th>作者</th>
 		<th>出版社</th>
 		<th>出版年月</th>
 		<th>图片</th>
 		<th>操作</th>
 	</tr>
";
foreach ($data as $value)
    {
        $str=$str."
                    <tr id='{$value['book_id']}'>
                        <td><input type='checkbox' name='checkall' value='{$value['book_id']}'></td>
                        <td>{$value['book_id']}</td>
                        <td>{$value['bookName']}</td>
                        <td>{$value['bookWriter']}</td>
                        <td>{$value['bookPress']}</td>
                        <td>{$value['bookYear']}</td>
                        <td><img src='./load/{$value['book_image']}' width='50px' height='50px'></td>
                        <td>
                            <a href='table.php'>返回首页</a>
                            <a href='delete.php?id={$value['book_id']}'>删除</a>
                        </td>
                    </tr>
                  ";
    }
$str.="</table>";
echo $str;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
原生 JS 和 AJAX 搜索功能可以通过以下步骤实现: 1. 创建一个 HTML 表单,其中包含一个输入框和一个提交按钮。 ```html <form> <input type="text" id="searchInput" placeholder="Search..."> <button type="submit" id="searchBtn">Search</button> </form> ``` 2. 为提交按钮添加一个事件监听器,当点击按钮时,阻止默认表单提交行为并调用搜索函数。 ```javascript document.getElementById("searchBtn").addEventListener("click", function(event) { event.preventDefault(); search(); }); ``` 3. 编写搜索函数,该函数使用 AJAX 发送 GET 请求到服务器,并将搜索结果显示在页面上。 ```javascript function search() { // 获取输入框的值 var query = document.getElementById("searchInput").value; // 创建 AJAX 请求 var xhr = new XMLHttpRequest(); xhr.open("GET", "/search?q=" + query, true); // 设置响应处理函数 xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { // 将搜索结果显示在页面上 var results = JSON.parse(xhr.responseText); displayResults(results); } }; // 发送请求 xhr.send(); } function displayResults(results) { // 将搜索结果显示在页面上 } ``` 4. 在服务器端实现搜索功能,接受 GET 请求中的查询参数并返回匹配的结果。可以使用 PHP、Node.js、Python 等服务器端语言实现。 以上就是一个简单的原生 JS 和 AJAX 搜索功能的实现方式。当然,具体实现方式还需要根据实际情况进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值