简体   繁体   English

按下按钮PHP后页面上升

[英]Page goes up after pressing button PHP

I used a calendar from the internet which uses PHP. 我使用了使用PHP的互联网日历。 It has buttons like "next" and "prev", and when I press one of them it takes me to the top of the page. 它具有“下一个”和“上一个”之类的按钮,当我按下其中一个按钮时,它将带我到页面顶部。 How can I fix that? 我该如何解决?

This is the code where I create the calendar: 这是我创建日历的代码:

    return
        '<div class="header">'.
            '<a class="prev" href="'.$this->naviHref.'?
            month='.sprintf('%02d',$preMonth).'&year='.$preYear.'">Prev</a>'.
           '<span class="title">'.date('Y M',strtotime($this->currentYear.'-'.$this->currentMonth.'-1')).'</span>'.
           '<a class="next" href="'.$this->naviHref.'?month='.sprintf("%02d", $nextMonth).'&year='.$nextYear.'">Next</a>'.
        '</div>';
    }

And here calendar.php 在这里calendar.php

https://ideone.com/SkECOp https://ideone.com/SkECOp

I think the problem lies not in your updated code, but in assigning $_SERVER['PHP_SELF'] to naviHref in the constructor. 我认为问题不在于您更新的代码,而在于将$_SERVER['PHP_SELF']分配给构造函数中的naviHref

public function __construct() {
    $this->naviHref = htmlentities($_SERVER['PHP_SELF']);
}

Read the $_SERVER['PHP_SELF'] value in the script where you create the Calendar instance and pass that value as argument to the Calendar constructor. 在创建Calendar实例的脚本中读取$_SERVER['PHP_SELF']值,并将该值作为参数传递给Calendar构造函数。 In which it will be assigned to naviHref . 在其中将其分配给naviHref

Also, the first two lines inside show() are raising notices 另外, show()中的前两行正在引起注意

$year == null;
$month == null;

Change them to 更改为

$year = null;
$month = null;

Edit : 编辑

In order to use your calender component, you have to create an instance of it, an object of Calendar type, like this: 为了使用日历组件,您必须创建它的一个实例,即Calendar类型的对象,如下所示:

$calendar = new Calendar();

This takes place in a php file. 这发生在一个php文件中。

But before doing this step, assign the value of $_SERVER['PHP_SELF'] to a variable. 但是在执行此步骤之前,请将$_SERVER['PHP_SELF']的值分配给变量。 Like this: 像这样:

$naviHref = htmlentities($_SERVER['PHP_SELF']);

And after that create your object of type Calendar , by passing it the variable, as argument. 然后,通过将变量作为参数传递给它,创建Calendar类型的对象。 Like this: 像这样:

$calendar = new Calendar($naviHref);

So, in the end you will have the following code in your php page: 因此,最后,您的php页面中将包含以下代码:

$naviHref = htmlentities($_SERVER['PHP_SELF']);
$calendar = new Calendar($naviHref);

echo $calendar->show();

After this, go to the Calendar constructor and change it like this: 之后,转到Calendar构造函数并按如下所示进行更改:

public function __construct($naviHref) {
    $this->naviHref = $naviHref;
}

This way you injected the variable $naviHref into your object of class Calendar . 这样,您将变量$ naviHref注入到Calendar类的对象中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM