donlih2986 2019-07-30 16:05
浏览 281
已采纳

如何使用按钮更改页面URL

I am trying to do a next/previous button on my calendar to show the next/previous month.

Ideally, the best thing would be not to refresh the page, but in the first place I don't mind having to refresh the page.

I have got a calendar($m,$y) function in php that let me change the value of my calendar directly in the url. For exemple (it's an image) : http://localhost/VELO/index1.php?mois=3&year=2019

So basically, in this exemple, I would like my previous button to change the url to ".../index1.php?mois=2&year=2019"

I tried the following code (and many others) from one of the most answered question about this subject but it didn't worked :

<script>
function getQueryVariable(variable) {
     var query = url.substring(1);
     var vars = query.split('&');
     for (var i=0; i<vars.length; i++) {
          var pair = vars[i].split('=');
          if (pair[0] == variable) {
            return pair[1];
          }
     }

     return false;
  }

var url = 'http://localhost/VELO/index1.php?mois=3&year=2019'

var mois = getQueryVariable(url, 'mois');
var year = getQueryVariable(url, 'year');

var params = { 'mois':2, 'year':2019};
var new_url = 'http://localhost/VELO/index1.php?' + jQuery.param(params);

// With this in my <body> : 
// <button type="button" class="precedent"><a href="new_url">Prev</a></button>

</script>

But I am getting the following error :

The requested URL /VELO/new_url was not found on this server

In summary, what I am asking is :

  • Is the code I am using not appliable in this case ?
  • Is there an other way to change my url with a button ? (can I use $_GET['month'] ?)
  • Is it possible to do this without refreshing the page ?
  • 写回答

2条回答 默认 最新

  • drj14664 2019-08-04 16:31
    关注

    I found an answer to my question !

    At first, I tried the solution degreerichi sent me but it wasn't working really well. Here is the first code that is NOT WORKING :

    <script> // NOT WORKING
    $(function() {
    
        $('.precedent').click(function(e){
        e.preventDefault();
        var targetUrl = "http://localhost/VELO/index1.php?mois=6";        
        window.history.pushState(null, null, targetUrl);
        setCurrentPage(targetUrl);
        }); 
    
    });
    </script>
    

    My problem here (if anyone still want to solve/explain it) is that the url is changing but my calendar is not changing. For instance, when I click on my previous button I have this in my localhost (image). You can see that the url changed but the calendar isn't the one for June (mois=6).

    So here is how I solved the problem :

    <script> // WORKING
    $(function() {
    
        $('.precedent').click(function(e){
        e.preventDefault();
        var targetUrl = "http://localhost/VELO/index1.php?mois=6";       
        window.location = targetUrl;
        }); 
    
    });
    </script>
    

    In this case my calendar is updating as follow this image.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图