dongzhaoshi8497 2014-07-20 02:11
浏览 61
已采纳

从包含的页面打开超链接而不更改页面

I have one principal page with two links, and one switch to get those links, and create includes for both:

Links

<div id="g-menu">
<ul>
<li> <a href="?link=1"  name="link1">Home</a></li>
<li> <a href="?link=2"  name="link1">Calendar</a></li>
</ul>
</div>

Swith

 switch ((isset($_GET['link']) ? $_GET['link'] : '')) {
  case "1":
    include('modules/groups/principal_page.php');
    break;
  case "2":
    $current_month = date('m') - 1; //check the current month
    $_GET['id']= $current_month; 
    $_GET['year']=date('Y'); //check the current year
    include 'modules/groups/calendar.php';
    break;
  default:
   include('modules/groups/principal_page.php');
 }

The first page is the principal page, the second one, is a page with an calendar.

To do the calendar buttons(back and forward), i used parameters, example, if i want to check the April in 2004 i do that:

calendar.php?id=4&year=2004 //4 because january starts at 0

The problem is that i'm using parameters to do that, and i dont know how to connect the buttons from the links, to the file that the calendar page is being included.

Code from the buttons

<?php } 

$id = preg_replace('#[^0-9]#i', '', $_GET['id']); 

$get_year = preg_replace('#[^0-9]#i', '', $_GET['year']); 

$back= $id - 1;
$back2 = $get_year;
$next= $id + 1;

if($back == 0 || $back == -1){
  $back = 12;
  $back2 = $get_year - 1;
}

if($id ==12){
  ++$get_year;
  $next= 1;
}


 ?>
<a href="calendar.php?id=<?= $back?>&year=<?= $back2; ?>"><button class="offersbtn">Back</button></a>
<a href="calendar.php?id=<?= $next?>&year=<?= $get_year; ?>"><button class="offersbtn">Next</button></a>

</div>
<br>
<?php
  echo get_date($id,$get_year);
?>

How can I use the navigation buttons without changing the current page that the calendar is being included?

  • 写回答

1条回答 默认 最新

  • dongma2388 2014-07-20 03:52
    关注

    Since you are using php, you'll have to implement some kind of client side scripting to make requests without reloading the page your on.

    I would suggest using jquery if you are not to sharp on Javascript.

    http://jquery.com/

    This is similar to a project I have completed.

    Put your output of calendar.php in a div, that your comfortable with the content being replaced and give it an id.I would not suggest using an iFrame because it will limit the functionality of your app. In most cases once your in an iFrame you can't get out.

    <div id=calendar><!---Contents of calendar.php---></div>
    

    Then create forms for your buttons passing your variables data through hidden input fields.

    <form id="back" method="GET" >
    <input name="id" type="hidden" value="<?= $back ?>"/>
    <input name= "year" type="hidden" value="<?= $back2; ?>"/>
    <button type="button" id="back_btn"class="offersbtn">Back</button>
    </form>
    
    <form id="next" method="GET">
    <input name="id" type="hidden" value="<?= $next ?>"/>
    <input name= "year" type="hidden" value="<?= $get_year; ?>"/>
    <button type="button" id="next_btn" class="offersbtn">Next</button>
    </form>
    

    Finally setup place some Jquery functions at the header of your file. (don't forget to include the library)

     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
     <script>
     $(function(){
        $(document).on("click","#back_btn",function() {
            $.ajax( {
                type: $(this).attr('method'),
                url: "calendar.php",
                data: $('#back').serialize(),
                success: function( response ) {
                    $( "#calendar" ).replaceWith( response );
                }
            });
        });
     });
    $(function(){
        $(document).on("click","#next_btn",function() {
            $.ajax( {
                type: $(this).attr('method'),
                url: "calendar.php",
                data: $('#next').serialize(),
                success: function( response ) {
                    $( "#calendar" ).replaceWith( response );
                }
            });
        });
     });
    
    </script>
    

    I'm sure there are many different ways with different frameworks and client side scripting. Hopefully this can get you off to a start. This is just one of the ways I have done it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看