如果单击按钮,如何编辑 PHP 变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17314926/
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 12:40:13  来源:igfitidea点击:

How can I edit a PHP variable if i click on a button?

phpjavascript

提问by martini1993

I would like to know how I change a PHP variable if I click on a button. Let's say I have the following webpage:

如果单击按钮,我想知道如何更改 PHP 变量。假设我有以下网页:

_______________________________________________
|   Menu    |         A Webpage                |
|           |                                  |
| - item1   |                                  |
| - item2   |                                  |
| - item3   |          Content                 |
| - item4   |                                  |
|           |                                  |
|           |                                  |

And if I click on 'item1' the page changes with the data that is related to the user that is linked with that button. So does it with the other items.

如果我单击“item1”,页面会随着与该按钮链接的用户相关的数据而变化。其他项目也是如此。

This is currently my code:

这是目前我的代码:

<script>
function test1()
{
alert("Hello World1!");
}
function test2()
{
alert("Hello World2!");
}
function test3()
{
alert("Hello World3!");
}
function test4()
{
alert("Hello World4!");
}


</script>


    <a id="test1" href="#" onClick="test1();"><h5>item1</h5></a><br /><br />
    <a id="test2" href="#" onClick="test2();"><h5>item2</h5></a><br /><br />
    <a id="test3" href="#" onClick="test3();"><h5>item3</h5></a><br /><br />
    <a id="test4" href="#" onClick="test4();"><h5>item4</h5></a><br /><br />

This is is enough to let a window appear or to hide a division or something. But what I want is if a button is clicked I want to change a PHP variable. Like this:

这足以让一个窗口出现或隐藏一个部门或其他东西。但我想要的是,如果单击按钮,我想更改 PHP 变量。像这样:

<script>
function test1()
{
<? $userid == '1'; ?>
}
</script>


    <a id="test1" href="#" onClick="test1();"><h5>item1</h5></a><br /><br />

With this I can easily change the page to what the user wants to see, without the need to make multiple pages. But my question is: 'how can I make this possible'?

有了这个,我可以轻松地将页面更改为用户想要查看的内容,而无需制作多个页面。但我的问题是:“我怎样才能做到这一点”?

I would like to thank you all in advance!

我要提前感谢大家!

回答by Chris

PHP is not the same language or type of language as JavaScript. PHP runs in a linear manner and once processing is complete, you can't go back and call a function again or update a variable for example. PHP runs code from the top of a file to the bottom of a file and when it hits the bottom it ends.

PHP 不是与 JavaScript 相同的语言或语言类型。PHP 以线性方式运行,一旦处理完成,您就无法返回并再次调用函数或更新变量。PHP 从文件顶部到文件底部运行代码,当它到达底部时结束。

The closest to the solution you are looking for would be to use jQuery AJAXto call a PHP script to update variables. It looks to me that you want to use AJAX to change the pages data. You could achieve this using the following example.

最接近您正在寻找的解决方案是使用jQuery AJAX调用 PHP 脚本来更新变量。在我看来,您想使用 AJAX 来更改页面数据。您可以使用以下示例实现此目的。

$('#menu A').click(function (e) {
    var jqxhr = $.get('phpscript.php?data=' + $(this).attr('href'));

    jqxhr.success(function(data) {
        $('#content').html(data);
    });
});

What the above will do is when you click on a link within <div id="menu" />it will attempt to get the content from a script running in the same directory called phpscript.php. This uses the data variable to determine what page data to return. Once complete, it will output the HTML returned by your script into the #content container, e.g. <div id="content">

上面会做的是,当您单击其中的链接时<div id="menu" />,它将尝试从运行在同一目录中的脚本中获取内容,该脚本名为 phpscript.php。这使用数据变量来确定要返回的页面数据。完成后,它会将您的脚本返回的 HTML 输出到 #content 容器中,例如<div id="content">

I hope I have understood your question correctly. And also keep in mind that this would need a little validation, checking you get a 200 OK and there is actually data received etc.

我希望我已经正确理解了你的问题。还要记住,这需要一点验证,检查你是否得到 200 OK 并且实际收到数据等。

回答by shanavas

I dont get why you want to change the value of the php variable

我不明白为什么要更改 php 变量的值

because this variable will not exist after execution of this page. where as you can use some session variable

因为这个页面执行后这个变量将不存在。你可以在哪里使用一些会话变量

also if we want to set some valus we a have to assignment operator (=)

此外,如果我们想设置一些值,我们必须赋值运算符 (=)

for setting a php variable you can use ajax call, in server there should be a page to change the value

要设置 php 变量,您可以使用 ajax 调用,在服务器中应该有一个页面来更改值

回答by Wallack

Use Ajax, this way you could load the content of a webpage inside something, like a div in your example, without needing to reload the entire page.

使用 Ajax,这样您就可以在某些内容中加载网页的内容,例如示例中的 div,而无需重新加载整个页面。

Ajax & PHP

Ajax 和 PHP

回答by amulous

You'll need to use Ajax requests to do this.

您需要使用 Ajax 请求来执行此操作。