如何为每个勾选的复选框创建一个递增 1 的变量
How to create a variable which increments by 1 for each checkbox which is ticked
我正在尝试创建一个函数,当勾选复选框时,该函数 returns 一个布尔值(1 或 0)....这里没有问题。我还希望它包含一个会话变量,每个选中的复选框都会递增 1。当我在提交表单 $_SESSION ['counter'] 后回显 $_SESSION ['counter'] 的值时,它仍然为零或我最初设置的任何值。谁能看到我哪里出错了?非常感谢您的提前帮助!
<?php
if(!isset($_SESSION))
{
session_start();
}
$_SESSION['counter']= 0;
function checkbox_boolean ($checkboxID){
$checkboxID= 0;
if(isset($_POST[$checkboxID])){
$checkboxID = 1;
$_SESSION['counter']++;
}
}
if(isset($_POST['submit'])){
echo $_SESSION['counter'];
checkbox_boolean ('slot1');
checkbox_boolean ('slot2');
checkbox_boolean ('slot3');
checkbox_boolean ('slot4');
checkbox_boolean ('slot5');
checkbox_boolean ('slot6');
checkbox_boolean ('slot7');
checkbox_boolean ('slot8');
}
?>
您正在设置
$_SESSION['counter']= 0;
每次调用 PHP 脚本时 归零。不管之前是什么,这条线总是将它重置为零。
将其更改为仅在它尚不存在时对其进行初始化。有点像..
if (!isset($_SESSION['counter'])) {
$_SESSION['counter']= 0;
}
您的代码中有不少错误,请参阅建议的修改
?php
// dont need to test anything just call session_start()
session_start();
// initialise only if not already in existance
if ( !isset( $_SESSION['counter'] ) ) {
$_SESSION['counter'] = 0;
}
function checkbox_boolean ($checkboxID){
// this just destroyed whatever was passed in the $checkboxID parameter
// $checkboxID= 0;
if(isset($_POST[$checkboxID])){
// This does nothing useful
//$checkboxID = 1;
$_SESSION['counter']++;
}
}
if(isset($_POST['submit'])){
echo 'Session counter before = ' . $_SESSION['counter'];
checkbox_boolean ('slot1');
checkbox_boolean ('slot2');
checkbox_boolean ('slot3');
checkbox_boolean ('slot4');
checkbox_boolean ('slot5');
checkbox_boolean ('slot6');
checkbox_boolean ('slot7');
checkbox_boolean ('slot8');
echo 'Session counter after = ' . $_SESSION['counter'];
}
?>
我正在尝试创建一个函数,当勾选复选框时,该函数 returns 一个布尔值(1 或 0)....这里没有问题。我还希望它包含一个会话变量,每个选中的复选框都会递增 1。当我在提交表单 $_SESSION ['counter'] 后回显 $_SESSION ['counter'] 的值时,它仍然为零或我最初设置的任何值。谁能看到我哪里出错了?非常感谢您的提前帮助!
<?php
if(!isset($_SESSION))
{
session_start();
}
$_SESSION['counter']= 0;
function checkbox_boolean ($checkboxID){
$checkboxID= 0;
if(isset($_POST[$checkboxID])){
$checkboxID = 1;
$_SESSION['counter']++;
}
}
if(isset($_POST['submit'])){
echo $_SESSION['counter'];
checkbox_boolean ('slot1');
checkbox_boolean ('slot2');
checkbox_boolean ('slot3');
checkbox_boolean ('slot4');
checkbox_boolean ('slot5');
checkbox_boolean ('slot6');
checkbox_boolean ('slot7');
checkbox_boolean ('slot8');
}
?>
您正在设置
$_SESSION['counter']= 0;
每次调用 PHP 脚本时 归零。不管之前是什么,这条线总是将它重置为零。
将其更改为仅在它尚不存在时对其进行初始化。有点像..
if (!isset($_SESSION['counter'])) {
$_SESSION['counter']= 0;
}
您的代码中有不少错误,请参阅建议的修改
?php
// dont need to test anything just call session_start()
session_start();
// initialise only if not already in existance
if ( !isset( $_SESSION['counter'] ) ) {
$_SESSION['counter'] = 0;
}
function checkbox_boolean ($checkboxID){
// this just destroyed whatever was passed in the $checkboxID parameter
// $checkboxID= 0;
if(isset($_POST[$checkboxID])){
// This does nothing useful
//$checkboxID = 1;
$_SESSION['counter']++;
}
}
if(isset($_POST['submit'])){
echo 'Session counter before = ' . $_SESSION['counter'];
checkbox_boolean ('slot1');
checkbox_boolean ('slot2');
checkbox_boolean ('slot3');
checkbox_boolean ('slot4');
checkbox_boolean ('slot5');
checkbox_boolean ('slot6');
checkbox_boolean ('slot7');
checkbox_boolean ('slot8');
echo 'Session counter after = ' . $_SESSION['counter'];
}
?>