从 <a href=""> 链接更改 PHP 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11319180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:15:52  来源:igfitidea点击:

Changing PHP variable from an <a href=""> link

phphtml

提问by aener

I'm making a site using divs. I have one across the top with a row of links which I want to change what's in the main window. I would prefer to keep strictly to PHP and HTML and use a full page refresh as I am comfortable with them at current, rather than using an Ajax refresh as I don't have any knowledge of Ajax.

我正在使用 div 制作网站。我在顶部有一个带有一排链接的链接,我想更改主窗口中的内容。我更愿意严格遵守 PHP 和 HTML 并使用整页刷新,因为我目前对它们感到满意,而不是使用 Ajax 刷新,因为我对 Ajax 一无所知。

I don't want to use iframes. I thought the best way to do this would be as follows:

我不想使用 iframe。我认为最好的方法如下:

<div id="top">
<a href="news.html">News</a>
</div>
<div id="main">
<?php include($page); ?>
</div>

What I need is a way of changing the $pagevariable upon clicking the link. If this means changing the variable and then reloading the page then so be it.

我需要的是一种$page在单击链接时更改变量的方法。如果这意味着更改变量然后重新加载页面,那就这样吧。

A command following the logic of:

遵循以下逻辑的命令:

<a href="news.html" action="<?php $page="news.html"; ?>">News</a>

would be ideal! I thought about using a form, but it seems there should be an easier way.

将是理想的!我想过使用表单,但似乎应该有更简单的方法。

回答by Quentin

You can't changea variable, per se. Reloading the page means running the PHP program from scratch. You just have to provide it with different input. From a regular link, that is only possible through the URL.

你不能改变变量本身。重新加载页面意味着从头开始运行 PHP 程序。你只需要为它提供不同的输入。从常规链接,这只能通过 URL 实现。

<a href="foo.php?something=a_value">

Data in the query string is accessible via $_GET

查询字符串中的数据可通过 $_GET

echo htmlspecialchars($_GET['something']);

回答by Mike

you could use a GET request on the same page,

您可以在同一页面上使用 GET 请求,

<a href="news.html?page=news.html">News</a>

This will cause a page refresh on click, with the value for the page variable passed to php, and accessible through

这将导致点击页面刷新,页面变量的值传递给 php,并可以通过访问

$page = $_GET['page'] //at this point, $page will be "news.html"

回答by André Catita

If I understood correctly, to change the include on the fly, as you are talking, you need to use AJAX, otherwise you need a full refresh.

如果我理解正确,要动态更改包含,正如您所说,您需要使用 AJAX,否则您需要完全刷新。

Send data back to the server, with a normal link.

使用正常链接将数据发送回服务器。

<a href="page.php?page=newpage.html">link</a>

Then you can use the $_GET variable of PHP, which is a array that parse the parameters for a URL page.

然后你可以使用PHP的$_GET变量,它是一个解析URL页面参数的数组。

Example:

例子:

$page = $_GET['page']
include $page;

If you use jQuery, or want to try a easy AJAX method, you can search for jQuery.load().

如果您使用 jQuery,或者想尝试一个简单的 AJAX 方法,您可以搜索jQuery.load()

回答by reformed

You could also use

你也可以使用

    $page = $_SERVER['PHP_SELF'];

At the top of the PHP script. If your page is "somepage.php" the above will output

在 PHP 脚本的顶部。如果您的页面是“somepage.php”,上面将输出

    /somepage.php

as the value of $page.

作为 $page 的值。

To get rid of the slash and .php just use substr:

要去掉斜杠和 .php 只需使用 substr:

    $page = substr($_SERVER['PHP_SELF'],1,-4);

which will leave you with $page = somepage

这会给你留下 $page = somepage