Php 如何使用keyup事件使分页在搜索结果中正常工作?
我正在使用keyup()函数将变量发送到一个php页面(consult.php),该页面包含mysql select以显示我的搜索结果 index.phpPhp 如何使用keyup事件使分页在搜索结果中正常工作?,php,jquery,mysql,Php,Jquery,Mysql,我正在使用keyup()函数将变量发送到一个php页面(consult.php),该页面包含mysql select以显示我的搜索结果 index.php <html> <head> <script type="text/javascript" src="jquery-1.7.2.min.js"></script> <script type="text/javascript"> $(function(){ $('#input').ke
<html>
<head>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function(){
$('#input').keyup(function(){
var inputVal=$('#input').val();
$.ajax({
type: 'POST',
data: ({word : inputVal}),
url: 'consult.php',
success: function(data) {
$('#content').html(data);
}
});
});
$("#menu li").click(function(){
$.ajax({
type: 'POST',
data: ({word : inputVal}),
url: 'consult.php',
success: function(data) {
$('#content').html(data);
}
});
});
});
</script>
<style>
#menu ul{
margin:0;
padding:0;
list-style:none;
}
#menu ul li{
padding:5px;
margin-right:2px;
float:left;
border:#00F solid 1px;
background-color:#faa8ad;
}
</style>
</head>
<body>
<div>Write<br>
<input type="text" id="input" name="input"/>
</div>
<div id="content"></div>
</body>
</html>
<html>
<head>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
function ajaxCall(inPage,inWord)
{
$.ajax({
type: 'POST',
data: ({word : inWord, pag : inPage}),
url: 'consult.php',
success: function(data) {
$('#content').html(data);
}});
}
$(function(){
$('#input').keyup(function(){
var inputVal=$('#input').val();
$.ajax({
type: 'POST',
data: ({word : inputVal}),
url: 'consult.php',
success: function(data) {
$('#content').html(data);
}
});
});
});
</script>
<style>
#menu ul{
margin:0;
padding:0;
list-style:none;
}
#menu ul li{
padding:5px;
margin-right:2px;
float:left;
border:#00F solid 1px;
background-color:#faa8ad;
}
</style>
</head>
<body>
<div>Write<br>
<input type="text" id="input" name="input"/>
</div>
<div id="content"></div>
</body>
</html>
$(函数(){
$('#input').keyup(函数(){
var inputVal=$('#input').val();
$.ajax({
键入:“POST”,
数据:({word:inputVal}),
url:'consult.php',
成功:功能(数据){
$('#content').html(数据);
}
});
});
$(“#菜单li”)。单击(函数(){
$.ajax({
键入:“POST”,
数据:({word:inputVal}),
url:'consult.php',
成功:功能(数据){
$('#content').html(数据);
}
});
});
});
#菜单ul{
保证金:0;
填充:0;
列表样式:无;
}
#菜单ulli{
填充物:5px;
右边距:2px;
浮动:左;
边框:#00F实心1px;
背景色:#faa8ad;
}
写
consult.php
<?
include('conn.php');
$word = isset($_POST['word']) ? $_POST['word'] : $_GET['word'];
if($word){
$perPag = 4;
$query = mysql_query("SELECT COUNT(carro_id) FROM carros WHERE carro_modelo LIKE '$word%'");
$totalResult = ceil(mysql_result($query, 0)/$perPag);
$pag = (isset($_GET['pag']) and (int)$_GET['pag'] > 0) ? (int)$_GET['pag'] : 1;
$ini = ($pag - 1) * $perPag;
$queryTotal = mysql_query("SELECT carro_modelo FROM carros WHERE carro_modelo LIKE '$word%' ORDER BY carro_id LIMIT $ini, $perPag");
while($row = mysql_fetch_assoc($queryTotal)){
echo "<div>".$row['carro_modelo']."</div>";
}
if($totalResult >= 1 && $pag <= $totalResult){
for($i = 1; $i <= $totalResult; $i++){
echo($i == $pag) ? '<div id="menu"><ul><li><strong><a href="?pag='.$i.'&word='.$word.'">'.$i.'</a></strong></li></ul></div> ' : '<div id="menu"><ul><li><a href="?pag='.$i.'&word='.$word.'">'.$i.'</a></li></ul></div> ';
}
}
}else{
$queryTotalReult = mysql_query("SELECT * FROM carros");
while($rowTotal = mysql_fetch_assoc($queryTotalReult)){
echo "<div>".$rowTotal['carro_modelo']."</div>";
}
}
?>
<?
include('conn.php');
$word = isset($_POST['word']) ? $_POST['word'] : $_GET['word'];
if($word){
$perPag = 4;
$query = mysql_query("SELECT COUNT(carro_id) FROM carros WHERE carro_modelo LIKE '$word%'");
$totalResult = ceil(mysql_result($query, 0)/$perPag);
$pag = (isset($_GET['pag']) and (int)$_GET['pag'] > 0) ? (int)$_GET['pag'] : 1;
$ini = ($pag - 1) * $perPag;
$queryTotal = mysql_query("SELECT carro_modelo FROM carros WHERE carro_modelo LIKE '$word%' ORDER BY carro_id LIMIT $ini, $perPag");
while($row = mysql_fetch_assoc($queryTotal)){
echo "<div>".$row['carro_modelo']."</div>";
}
if($totalResult >= 1 && $pag <= $totalResult){
for($i = 1; $i <= $totalResult; $i++){
echo($i == $pag) ? '<div id="menu"><ul><li><strong><a onclick="ajaxCall('.$i.',\''.$word.'\')">'.$i.'</a></strong></li></ul></div> ' : '<div id="menu"><ul><li><a onclick="ajaxCall('.$i.',\''.$word.'\')">'.$i.'</a></li></ul></div> ';
}
}
}else{
$queryTotalReult = mysql_query("SELECT * FROM carros");
while($rowTotal = mysql_fetch_assoc($queryTotalReult)){
echo "<div>".$rowTotal['carro_modelo']."</div>";
}
}
?>
在查询中有$initial,但在赋值中有$ini。也许可以试着把它们变成相同的:)index.php
<html>
<head>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function(){
$('#input').keyup(function(){
var inputVal=$('#input').val();
$.ajax({
type: 'POST',
data: ({word : inputVal}),
url: 'consult.php',
success: function(data) {
$('#content').html(data);
}
});
});
$("#menu li").click(function(){
$.ajax({
type: 'POST',
data: ({word : inputVal}),
url: 'consult.php',
success: function(data) {
$('#content').html(data);
}
});
});
});
</script>
<style>
#menu ul{
margin:0;
padding:0;
list-style:none;
}
#menu ul li{
padding:5px;
margin-right:2px;
float:left;
border:#00F solid 1px;
background-color:#faa8ad;
}
</style>
</head>
<body>
<div>Write<br>
<input type="text" id="input" name="input"/>
</div>
<div id="content"></div>
</body>
</html>
<html>
<head>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
function ajaxCall(inPage,inWord)
{
$.ajax({
type: 'POST',
data: ({word : inWord, pag : inPage}),
url: 'consult.php',
success: function(data) {
$('#content').html(data);
}});
}
$(function(){
$('#input').keyup(function(){
var inputVal=$('#input').val();
$.ajax({
type: 'POST',
data: ({word : inputVal}),
url: 'consult.php',
success: function(data) {
$('#content').html(data);
}
});
});
});
</script>
<style>
#menu ul{
margin:0;
padding:0;
list-style:none;
}
#menu ul li{
padding:5px;
margin-right:2px;
float:left;
border:#00F solid 1px;
background-color:#faa8ad;
}
</style>
</head>
<body>
<div>Write<br>
<input type="text" id="input" name="input"/>
</div>
<div id="content"></div>
</body>
</html>
函数ajaxCall(inPage,inWord)
{
$.ajax({
键入:“POST”,
数据:({word:inWord,pag:inPage}),
url:'consult.php',
成功:功能(数据){
$('#content').html(数据);
}});
}
$(函数(){
$('#input').keyup(函数(){
var inputVal=$('#input').val();
$.ajax({
键入:“POST”,
数据:({word:inputVal}),
url:'consult.php',
成功:功能(数据){
$('#content').html(数据);
}
});
});
});
#菜单ul{
保证金:0;
填充:0;
列表样式:无;
}
#菜单ulli{
填充物:5px;
右边距:2px;
浮动:左;
边框:#00F实心1px;
背景色:#faa8ad;
}
写
consult.php
<?
include('conn.php');
$word = isset($_POST['word']) ? $_POST['word'] : $_GET['word'];
if($word){
$perPag = 4;
$query = mysql_query("SELECT COUNT(carro_id) FROM carros WHERE carro_modelo LIKE '$word%'");
$totalResult = ceil(mysql_result($query, 0)/$perPag);
$pag = (isset($_GET['pag']) and (int)$_GET['pag'] > 0) ? (int)$_GET['pag'] : 1;
$ini = ($pag - 1) * $perPag;
$queryTotal = mysql_query("SELECT carro_modelo FROM carros WHERE carro_modelo LIKE '$word%' ORDER BY carro_id LIMIT $ini, $perPag");
while($row = mysql_fetch_assoc($queryTotal)){
echo "<div>".$row['carro_modelo']."</div>";
}
if($totalResult >= 1 && $pag <= $totalResult){
for($i = 1; $i <= $totalResult; $i++){
echo($i == $pag) ? '<div id="menu"><ul><li><strong><a href="?pag='.$i.'&word='.$word.'">'.$i.'</a></strong></li></ul></div> ' : '<div id="menu"><ul><li><a href="?pag='.$i.'&word='.$word.'">'.$i.'</a></li></ul></div> ';
}
}
}else{
$queryTotalReult = mysql_query("SELECT * FROM carros");
while($rowTotal = mysql_fetch_assoc($queryTotalReult)){
echo "<div>".$rowTotal['carro_modelo']."</div>";
}
}
?>
<?
include('conn.php');
$word = isset($_POST['word']) ? $_POST['word'] : $_GET['word'];
if($word){
$perPag = 4;
$query = mysql_query("SELECT COUNT(carro_id) FROM carros WHERE carro_modelo LIKE '$word%'");
$totalResult = ceil(mysql_result($query, 0)/$perPag);
$pag = (isset($_GET['pag']) and (int)$_GET['pag'] > 0) ? (int)$_GET['pag'] : 1;
$ini = ($pag - 1) * $perPag;
$queryTotal = mysql_query("SELECT carro_modelo FROM carros WHERE carro_modelo LIKE '$word%' ORDER BY carro_id LIMIT $ini, $perPag");
while($row = mysql_fetch_assoc($queryTotal)){
echo "<div>".$row['carro_modelo']."</div>";
}
if($totalResult >= 1 && $pag <= $totalResult){
for($i = 1; $i <= $totalResult; $i++){
echo($i == $pag) ? '<div id="menu"><ul><li><strong><a onclick="ajaxCall('.$i.',\''.$word.'\')">'.$i.'</a></strong></li></ul></div> ' : '<div id="menu"><ul><li><a onclick="ajaxCall('.$i.',\''.$word.'\')">'.$i.'</a></li></ul></div> ';
}
}
}else{
$queryTotalReult = mysql_query("SELECT * FROM carros");
while($rowTotal = mysql_fetch_assoc($queryTotalReult)){
echo "<div>".$rowTotal['carro_modelo']."</div>";
}
}
?>
您必须在页面提交时发布所有需要的参数,并使用搜索关键字和分页参数更改查询,或者使用ajax PaginationAnks获取帮助,您的意思是使用GET-use post?你能给我举个例子吗?我怎样才能做出这样的改变?我很欣赏下面一个洛蒂发布的代码,它可能会起作用,但我强烈建议大家看看这里发生了什么。实际上学习如何做比只尝试别人告诉你的要好。此外,在下一个/上一个链接中,你需要再次传递word,你还需要在php后端检查$_GET['word']。谢谢你纠正我,但这不是问题所在。;)是的,但我不明白你说的“下一步/回来”是什么意思?基于我的代码,你能给我举个例子吗?consult.php与index.php相同,唯一的变化是,我不带所有结果,只带输入字段页中键入的内容:1、2、3、4,您还需要检查isset($\u POST['word'])或isset($\u GET['word']),因为现在您将通过点击这些链接提交word。不,你有?pagina=$i,但你也需要&word=$word当我在分页时点击2,结果消失了。哦,是的,你需要附加一个click事件来进行与keyup事件相同的AJAX调用。因此,它不应该在某个地方链接,而应该调用一个Javascript函数来提取记录,就像在keyup中一样代码>没有工作!我知道你最近已经做了,但是你能在你原来的帖子中再次更新你的代码以准确反映你目前正在使用的代码吗?我也更新了我的代码。也试试我的两个文件,看看是否有帮助。