我需要som帮助使用提交按钮向会话变量添加值。如果我将变量定义为0则不会计数,如果我不这样做,则说:
“注意:未定义的索引:paperbagCount in 第52行/home/saxon/students/20151/chwi15/www/affar3/test.php“
“注意:未定义的索引:plasticbagCount in 第59行/home/saxon/students/20151/chwi15/www/affar3/test.php“
<?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>
答案 0 :(得分:1)
用这个替换你的PHP代码
<?php
@session_start(); // @ to avoid warning
if (!isset($_SESSION["paperbagCount"])) {
$_SESSION["paperbagCount"] = 0;
}
if (!isset($_SESSION["plasticbagCount"])) {
$_SESSION["plasticbagCount"] = 0;
}
?>
答案 1 :(得分:0)
session_start()
根据通过GET或POST请求传递的会话标识符创建会话或恢复当前会话,或通过cookie传递。
因此请从代码中删除第二行session_start()
。