【发布时间】:2023-03-31 18:51:01
【问题描述】:
我需要使用提交按钮向会话变量添加值的帮助。如果我将变量定义为 0,它就不会计数,如果我不这样做,它会说:
"注意:未定义索引:paperbagCount in /home/saxon/students/20151/chwi15/www/affar3/test.php 在第 52 行"
"注意:未定义索引:plasticbagCount in /home/saxon/students/20151/chwi15/www/affar3/test.php 第 59 行"
<?php
if(!session_status() === PHP_SESSION_ACTIVE ) {
session_start();
$_SESSION["paperbagCount"] = 0;
$_SESSION["plasticbagCount"] = 0;
}
session_start();
error_reporting(-1);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Startsida</title>
</head>
<body>
<?php
if(isset($_POST['addPaper'])) {
$_SESSION["paperbagCount"]+=1;
}
if(isset($_POST['deletePaper'])) {
if ($_SESSION["paperbagCount"] == 0) {
$_SESSION["paperbagCount"] == 0;
}else{
$_SESSION["paperbagCount"]-=1;
}
}
if(isset($_POST['addPlastic'])) {
$_SESSION["plasticbagCount"]+=1;
}
if(isset($_POST['deletePlastic'])) {
if ($_SESSION["plasticbagCount"] == 0) {
$_SESSION["plasticbagCount"] == 0;
}else{
$_SESSION["plasticbagCount"]-=1;
}
}
?>
<form method="post">
<p><label>Paperbagcount</label><br>
<input type="submit" name="addPaper" value="+">
<?php echo $_SESSION["paperbagCount"] . "\n"; ?>
<input type="submit" name="deletePaper" value="-">
</form>
<form method="post">
<p><label>Plasticbagcount</label><br>
<input type="submit" name="addPlastic" value="+">
<?php echo $_SESSION["plasticbagCount"] . "\n"; ?>
<input type="submit" name="deletePlastic" value="-">
</form>
</body>
</html>
【问题讨论】:
-
你写 session_start();两次在您的页面中删除以下一次
标签: php variables session submit counter