我尝试创建一个变量来存储按钮点击的计数。不幸的是,我收到了以下错误:
有人知道我做错了什么吗?
Undefined variable: counter
这是我的代码:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$counter = isset($_POST['counter']) ? $_POST['counter'] : 0;
if(isset($_POST["button"])){
$counter++;
echo $counter;
}
}
这是一个表单:
<form action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method = post>
<input type = "submit" name = "button" value = "Submit" >
<input type = "hidden" name = "counter" value = "<?php print $counter; ?>"; />
</form>
有人知道我做错了什么吗?