我的 PHP 函数只在第一次点击时减少按钮,为什么?

huangapple go评论78阅读模式
英文:

Why does my PHP function only decrement my button on the first click?

问题

When I click on my button it removes 1 from $decr but if I click again it does not remove anything:

<?php

function myFunction() {
    global $decr;
    $decr--;
}

if (isset($_GET['name']) && $_GET['name'] === 'true') {
    $decr--;
    $_GET['name'] = 'false';
}

?>

<a href='test.php?name=true' class="button-56"><?php echo $decr; ?></a>

I made a button, and I created my PHP function to decrement 1, but when I click a second time, it does not remove anything.

英文:

When I click on my button it removes 1 from $decr but if I click again it does not remove anything:

        &lt;?php

function myFunction() {
    global $decr;
    $decr--;
}

if (isset($_GET[&#39;name&#39;]) &amp;&amp; $_GET[&#39;name&#39;] === &#39;true&#39;) {
    $decr--;
    $_GET[&#39;name&#39;] = &#39;false&#39;;
}

?&gt;

&lt;a href=&#39;test.php?name=true&#39; class=&quot;button-56&quot;&gt;&lt;?php echo $decr; ?&gt;&lt;/a&gt;

I made a button I made my php function to decrement 1 but when I click a second time it does not remove anything.

答案1

得分: 2

根据评论中的所有人提到的,网页默认情况下不会在请求之间保持数据。为了保持数据,我们使用诸如会话、cookie、数据库等技术来存储数据。

我们还可以在表单中使用隐藏字段来将数据传递给服务器。下面的代码演示了这一点。

<?php
// test.php

// 首先检查是否为POST请求
if ($_POST) {
    $decr = $_POST['decr'];// 从POST数据中读取当前值
    $decr--; // 减少它
} else {
    $decr = 10; // 如果不是POST请求,则用10初始化$decr。
} 
?>
<form action="test.php" method="post">
    <!-- 在隐藏字段中输入$decr的值 -->
    <input type="hidden" name="decr" value="<?php echo $decr;?>">
    <input type="button" value="<?php echo $decr;?>" onclick="submit()">
</form>
英文:

As everyone mentioned in their comments, the web pages doesn't persist data between requests by default. To persist data, we use techniques like session, cookie, databases etc... to store data.

We can also use hidden fields in the form to pass data to the server. The below code demonstrates it.

&lt;?php
// test.php

// at first check for a POST request
if ($_POST) {
    $decr = $_POST[&#39;decr&#39;];// read the current value from POST data
    $decr--; // decrement it
} else {
    $decr = 10; // if not a post request, initialize $decr with 10.
}
?&gt;
&lt;form action=&quot;test.php&quot; method=&quot;post&quot;&gt;
    &lt;!-- input the $decr value in a hidden field --&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;decr&quot; value=&quot;&lt;?php echo $decr;?&gt;&quot;&gt;
    &lt;input type=&quot;button&quot; value=&quot;&lt;?php echo $decr;?&gt;&quot; onclick=&quot;submit()&quot;&gt;
&lt;/form&gt;

答案2

得分: 1

PHP本身在请求之间不保留数据。global仅对一个响应有效,然后变量在下一个请求中以初始值重新创建。您只需要使用一些存储方式(例如cookie、会话、数据库等)。

英文:

PHP itself doesn't persist data between requests. global works only for one response, then variable recreated again on next request with initial value. All you need is use some storage (cookie, session, database, etc.)

答案3

得分: 0

请尝试这个,这是你正在尝试做的事情的一个很好的起点。当你希望在页面加载之间保持数值不变时,请使用会话。

<?php
// 这必须是页面上PHP看到的第一件事
session_start();

// 确保你在第一次创建变量,否则会得到一个PHP错误
if (!isset($_SESSION['decr'])) { 
    $_SESSION['decr'] = '0';
}

if (isset($_GET['name']) && $_GET['name'] === 'true') {
    $_SESSION['decr']--;
    $_GET['name'] = 'false';
}
?>
<a href='test.php?name=true' class="button-56">Num: <?php echo $_SESSION['decr']; ?></a>

希望这有所帮助。

英文:

Try this, it is a good start for what you are trying to do. Use sessions when you want to keep the value alive from page load to page load.

&lt;?php
// this has to be the first thing that php sees on the page
session_start();

// make sure you create the variable the first time or you will get a php error
if (!isset($_SESSION[&#39;decr&#39;])) { 
    $_SESSION[&#39;decr&#39;] = &#39;0&#39;;
}

if (isset($_GET[&#39;name&#39;]) &amp;&amp; $_GET[&#39;name&#39;] === &#39;true&#39;) {
    $_SESSION[&#39;decr&#39;]--;
    $_GET[&#39;name&#39;] = &#39;false&#39;;
}
?&gt;
&lt;a href=&#39;test.php?name=true&#39; class=&quot;button-56&quot;&gt;Num: &lt;?php echo $_SESSION[&#39;decr&#39;]; ?&gt;&lt;/a&gt;

huangapple
  • 本文由 发表于 2023年5月22日 03:05:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定