英文:
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:
<?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 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.
<?php
// test.php
// at first check for a POST request
if ($_POST) {
$decr = $_POST['decr'];// read the current value from POST data
$decr--; // decrement it
} else {
$decr = 10; // if not a post request, initialize $decr with 10.
}
?>
<form action="test.php" method="post">
<!-- input the $decr value in a hidden field -->
<input type="hidden" name="decr" value="<?php echo $decr;?>">
<input type="button" value="<?php echo $decr;?>" onclick="submit()">
</form>
答案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.
<?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['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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论