English 中文(简体)
添加 AJAX 添加 AJAX 时不起作用的挂挂挂
原标题:Pagination not working when adding AJAX

我有一个基于PHP的页码系统, 运作良好, 我使用 Get 参数通过页码 :

    <?php 
$db = mysql_select_db($database,$connection) or trigger_error("SQL", E_USER_ERROR);
$sql1 = "SELECT COUNT(*) FROM $table";
$result1 = mysql_query($sql1, $connection) or trigger_error("SQL", E_USER_ERROR);
$row = mysql_fetch_row($result1);
$numrows = $row[0];
$rowsperpage = 2;
$totalpages = ceil($numrows / $rowsperpage);
if (isset($_GET[ page ]) && is_numeric($_GET[ page ])) {
   $currentpage = (int) $_GET[ page ];
} else {
   $currentpage = 1;
}
if ($currentpage > $totalpages) {
   $currentpage = $totalpages;
}
if ($currentpage < 1) {
   $currentpage = 1;
}
$offset = ($currentpage - 1) * $rowsperpage;
$sql2 = "SELECT * FROM internet_security ORDER BY id DESC LIMIT $offset, $rowsperpage";
$result2 = mysql_query($sql2, $connection) or trigger_error("SQL", E_USER_ERROR);
$list = mysql_fetch_assoc($result2);
$startrow = ($currentpage-1) * $rowsperpage;

?>

我展示链接的方式是这样的:

    h3>Results <?php echo ($startrow+1) ?> - <?php echo min($startrow + $rowsperpage, $row) ?> of <?php echo ($totalpages *$rowsperpage) ?></h3>
<ul><?php 
if ($currentpage!=$totalpages) {
echo " <li><a href= {$_SERVER[ PHP_SELF ]}?page=$totalpages >$totalpages</a></li> ";
$nextpage = $currentpage + 1;
echo " <li><a href= {$_SERVER[ PHP_SELF ]}?page=$nextpage >Next&raquo;&raquo;</a></li> ";
}?></ul>



<ul><?php    
if($currentpage<$totalpages){
for ($x = ($currentpage - 3); $x < (($currentpage + 3) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo " <li id= pcurrent ><a href= {$_SERVER[ PHP_SELF ]}?page=$x >$x</a></li>";
} else {
echo " <li><a href= {$_SERVER[ PHP_SELF ]}?page=$x >$x</a></li> ";
}}}  
}

?> </ul>


<ul><?php
if ($currentpage > 1){
$prevpage = $currentpage - 1;
echo " <li><a href= {$_SERVER[ PHP_SELF ]}?page=$prevpage >&laquo;&laquo;Prev</a></li> ";
echo "<li><a href= {$_SERVER[ PHP_SELF ]}?page=1 >1</a></li> ";
}?></ul>

这个剖面法很正常

我的问题是 < 坚固> 我的疑问是 < / 坚固 > 我现在要添加 AJAX 的功能性, 这样我就可以在射程上同时具有两个功能, 也就是说, 如果 JavaScript 被关闭, 射程将在 PHP 中工作 。

我试过这个:

$(function() {
    $( #pagination ul li a, .temp ).click(function(ev) {
        ev.preventDefault();
        $( #temporary ).load($(this).attr( href )).modal();
    });
});

但是现在,页码没有起作用,当按下页码链接时,没有发生任何情况。怎么了?

最佳回答

似乎你的要求有些混乱。 据我所知,你既想让PHP的排程(当js被禁用时),也想让AJAX的排程(当启用 js 时) 来利用动态装载(当启用 js 时) 。

为此,您应当采取以下行动:

  1. Put the code that generates your list into a function such as generateList($page)
  2. In your php page, output the results of the above created function as your initial view.
  3. Create an ajax_actions.php page that can call the function you created in step 1 and return the result.
  4. Use the link click hander to call (via AJAX) the ajax_actions.php page and display the result. You will probably need to parse the page id from your link click and pass it along.

如果您做了这些事, 当 JS 禁用时, PHP 会处理页码问题 。 当 JS 启用时, 您会做 ev. 防止 Default (), 并使用 AJAX 显示内容 。

希望这有帮助。

问题回答

如果 JavaScript 不为浏览器支持, Ajax 将无效。 Ajax 代表 Asyncronous JavaScript 和 XML 。

所以,现在你还想支持阿贾克斯吗?

如果回答为 " 是 ",则确认如下:

  1. "是装有列表和页码的集装箱吗?"

  2. 给定的 URl( $( this).attr( href)) 发送的结果是什么? 它应该返回正确的 HTml, 没有文档和身体标签等 。

  3. 您检查了 JavaScript 错误控制台, 是否有错误?

您可以使用ajax 方法来提供处理服务器错误的函数, 而不是装入, 您可以查看服务器是否正在返回任何错误 。





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签