【问题标题】:how to create a link to next page (currentpage+1)如何创建指向下一页的链接 (currentpage+1)
【发布时间】:2017-03-27 02:24:35
【问题描述】:

我有 30 页。按数字排序 ?page1 ?page2 ?page3......?page30

假设现在,我留在 page=1 http://localhost/flowplayer/manga/manga_demo2.php?page=1

如果我想添加一个链接到下一页(当前页面+1)的按钮,我应该怎么做?

这是我的尝试,但没有成功。

<input type="button" onclick="location.href='<?php echo $_SERVER['REQUEST_URI'] + 1;?>';" value="Next">

【问题讨论】:

  • 您可以使用 $_GET['page'] 获取页码的当前值并将其加上一个...确保使用条形标签或 htmlspecialchars 清理字符串。谷歌清理 GET php

标签: javascript php html


【解决方案1】:

你真的可以不用任何php代码就可以做到

<button id="b">click</button>
<script>
var pageStr=location.href.split("page=")[1];
var pageId=Number(pageStr);
b.onclick=function(){
    location.href="http://localhost/flowplayer/manga/manga_demo2.php?page="+ ++pageId;
}
</script>

【讨论】:

    【解决方案2】:

    我想这样就可以了。 cmets中的解释:

    <?php
    
    /* Get the full URL */
    $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    
    /* Get the page number */
    $page = substr($url, -1);
    
    /* Check if it's really a number for security */
    if(!preg_match("/^[0-9]+$/", $page)){
        die("Invalid page number given!");
    }
    
    /* Remove the page number from the URL */
    $url = rtrim($url, $page);
    
    /* Count the page up with +1 */
    $page++;
    
    /* Generage the new url */
    $newurl = $url . $page;
    
    ?>
    
    <!-- Put it inside your button element -->
    
    <input type="button" onclick="location.href='<?php echo $newurl; ?>';" value="Next">
    

    【讨论】:

      【解决方案3】:
      <input type="button" onclick="location.href='<?php echo $_SERVER['REQUEST_URI'] + 1;?>';" value="Next">
      instead of this you can do :
      <input type="button" onClick="nextPage()"/>
      function nextPage()
      {
         var urlName=$_SERVER['REQUEST_URI'];
         var newUrlName=substr(urlName,0,urlName.length-6)."page=";
         var appendedValue=$_GET["page"]+1;
         location.href=newUrlName.appendedValue;
      
      }
      

      【讨论】:

      • 我希望你意识到你不能像那样在 Javascript 函数中调用 PHP 变量..
      猜你喜欢
      • 2015-06-05
      • 1970-01-01
      • 1970-01-01
      • 2013-05-07
      • 1970-01-01
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多