简体   繁体   English

重新加载页面并重置 URL

[英]Reload a page and reset URL

I am getting a list of items from a mySQL database table, however I have some filters on the page, these filters are responsable for changing values of the variables responsable for storing the ORDER BY and LIMIT values on the query.我正在从 mySQL 数据库表中获取项目列表,但是我在页面上有一些过滤器,这些过滤器负责更改负责在查询中存储ORDER BYLIMIT值的变量的值。

The way I do this is changing the location.href through JavaScript and the PHP variables get the data with $_GET[] .我这样做的方法是通过 JavaScript 更改location.href ,PHP 变量使用$_GET[]获取数据。 When $_GET[] is not set, for example, in the first access to the page, some pattern values are sent to the query.$_GET[]未设置时,例如在第一次访问页面时,会向查询发送一些模式值。

function itensPorPag() {

    let qtde = document.getElementById('select_itens').value;
    let order = document.getElementById('order_page').value;

    let offset = 0;

    let url = 'adm_conta.php?prod_qtde='+qtde+'&offset='+offset;

    location.href = url;

}

Original URL:原始网址:

adm_conta.php

URL after filters:过滤后的网址:

adm_conta.php?prod_qtde=4&offset=0

Setting variables:设置变量:

if (isset($_GET['prod_qtde'])) {
    $limit = $_GET['prod_qtde'];
    $offset = $_GET['offset'];
} else {
    $limit = '3';
    $offset = '0';
}

So far so good.到现在为止还挺好。 The problem starts when I need to reload the page without applying any filter, like pressing F5 , for instance.当我需要重新加载页面而不应用任何过滤器时,问题就开始了,例如按F5 It because if I just reload the page, the variables will receive the values from the URL, since $_GET[] is set.这是因为如果我只是重新加载页面,变量将从 URL 接收值,因为$_GET[]已设置。

URL after filters:过滤后的网址:

adm_conta.php?prod_qtde=4&offset=0

URL after reloading page (it doesn't change, since it is receiving the same values):重新加载页面后的 URL(它不会改变,因为它接收到相同的值):

adm_conta.php?prod_qtde=4&offset=0

I found a solution for this, but it is lacking a little part.我为此找到了解决方案,但缺少一点。 I am using the keyup event in JavaScript to change the URL when the page is reloaded by pressing F5 , CTRL + R and CTRL + F5 :我在 JavaScript 中使用keyup event来更改 URL,当页面通过按F5CTRL + RCTRL + F5重新加载时:

document.onkeyup = checkKey;

function checkKey(e) {

    if (e.keyCode == 82 && e.ctrlKey || e.keyCode == 116 && e.ctrlKey || e.keyCode == 116) {

        location.href = 'adm_conta.php';
    } 
}

It works very well.它运作良好。 But I also need to be able to do the same when reloading the page by clicking on the reload button, and that is what is missing.但是我还需要在通过单击重新加载按钮重新加载页面时能够执行相同的操作,这就是所缺少的。

This button:这个按钮:

在此处输入图片说明

I have tried to use window.onbeforeunload , but it will be triggered whenever the page reloads, what would trouble my application, since the page is reloaded after a filter being applied and in this case I can't reset the URL.我曾尝试使用window.onbeforeunload ,但它会在页面重新加载时触发,这会给我的应用程序带来什么麻烦,因为在应用过滤器后重新加载页面,在这种情况下我无法重置 URL。

How could I do that?我怎么能那样做?

I am oppened to suggestions, even if I have to change the way I am doing that.我愿意接受建议,即使我必须改变我这样做的方式。

Just use PHP function header()只需使用 PHP 函数header()

header('Location: http://www.example.com/');

So after setting ur variables所以在设置你的变量之后

if (isset($_GET['prod_qtde'])) {
$limit = $_GET['prod_qtde'];
$offset = $_GET['offset'];
} else {
    $limit = '3';
    $offset = '0';
}

and using them for you know what just send header to redirect user browser to same location but without url query.并使用它们让您知道什么只是发送标头以将用户浏览器重定向到同一位置但没有 url 查询。

Examples with usage:用法示例:

header("Refresh:0"); header("刷新:0");

=> refresh same page (maybe best solution) => 刷新同一页面(也许是最好的解决方案)

header("Refresh:0; url=adm_conta.php"); header("刷新:0; url=adm_conta.php");

=> refresh with redirect => 使用重定向刷新

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

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