【问题标题】:PHP: moving the checked elements(via checkbox) from one array to anotherPHP:将选中的元素(通过复选框)从一个数组移动到另一个数组
【发布时间】:2026-02-08 02:40:01
【问题描述】:
<form action = 'xx.php' method = 'POST'>

<?php

session_start();

if(!isset($_SESSION['personA']))
{
    $personA = array('Jack', 'Dave', 'Smith', 'Daniel', 'Peter');
    $personB = array('Tom');
}

else
{
    $personA = $_SESSION['personA'];
    $personB = $_SESSION['personB'];
}


if($_POST['submit1'])
{
$chk1 = $_POST["check1"];


foreach($chk1 as $ch)
{
    echo $ch;
}
}

echo '<br />';
echo '<br />';

if($_POST['submit2'])
{
$chk2 = $_POST["check2"];


foreach($chk2 as $ch)
{
    echo $ch;
}
}

echo '<br />';
echo '<br />';
?>

<html>
<body>
<form>

Table personA:
<fieldset style= "width: 200px;">
<?php
foreach($personA as $a)
echo '<input type = "checkbox" name = "check1[]">'.$a.'<br />';

echo '</fieldset>';
echo '<br />';
echo '<input type = "submit" name = "submit1">';
echo '<br />';
echo '<br />';
?>

Table personB:
<fieldset style= "width: 200px;">

<?php
foreach($personB as $a)
echo '<input type = "checkbox" name = "check2[]">'.$a.'<br />';

echo '</fieldset>';
echo '<br />';
echo '<input type = "submit" name = "submit2">';

echo '<br />';
echo '<br />';
print_r ($personA);
echo '<br />';
print_r ($personB);
?>
</form>
</body>
</html>

这是我希望它的工作方式:

-我选择(例如)Jack、Dave 和 Smith,方法是选中他们名字的复选框,然后按下提交按钮。他们从数组 PersonA 移动到数组 PersonB

-我通过选中他名字的复选框来选择(例如)Tom,然后按下提交按钮。他从数组 personB 移动到数组 personB

所以现在数组应该是这样的: $personA = array('丹尼尔', '彼得', '汤姆'); $personB = array('杰克', '戴夫', '史密斯');

-该程序允许我继续执行此操作,直到我按下任一提交按钮且未选中任何复选框。然后它应该显示两个数组中的每个人。

【问题讨论】:

    标签: php html arrays


    【解决方案1】:

    首先,您需要确保它们已初始化。之后,您需要跟踪所有值,因为自然行为是无状态的,因此您需要使用会话,并从那里获取这些会话中复选框的值。考虑这个例子:(我只是以index.php为例)

    <?php
    session_start();
    // initialize default values
    $personA = array('Jack', 'Dave', 'Smith', 'Daniel', 'Peter');
    $personB = array('Tom');
    
    // initializations
    if(!isset($_SESSION['personA'])) {
        $_SESSION['personA'] = $personA; 
    }
    
    if(!isset($_SESSION['personB'])) {
        $_SESSION['personB'] = $personB; 
    }
    
    // handle transfer a => b
    if(isset($_POST['submit1'])) {
        $check1 = $_POST['check1'];
        foreach($check1 as $key => $value) {
            $_SESSION['personB'][] = $value;
            $key = array_search($value, $_SESSION['personA']);
            unset($_SESSION['personA'][$key]);
        }
        header('Location: index.php');
    }
    
    // handle transfer b => a
    if(isset($_POST['submit2'])) {
        $check2 = $_POST['check2'];
        foreach($check2 as $key => $value) {
            $_SESSION['personA'][] = $value;
            $key = array_search($value, $_SESSION['personB']);
            unset($_SESSION['personB'][$key]);
        }
        header('Location: index.php');
    }
    
    // simple reset
    if(isset($_POST['reset'])) {
        unset($_SESSION['personA']);
        unset($_SESSION['personB']);
        header('Location: index.php');
    }
    
    ?>
    
    <!-- 
    The important part here is that, you loop the values of those sessions, not the POST values, because sessions are the ones that are saved.
    -->
    <form method="POST" action="index.php">
        <fieldset style="border: 1px solid gray; margin-bottom: 10px;">
            <?php foreach($_SESSION['personA'] as $key => $value): ?>
                <input type="checkbox" name="check1[]" value="<?php echo $value; ?>" /> <?php echo $value; ?>
            <?php endforeach; ?>
            <br/>
            <input type="submit" name="submit1" value="Transfer to B" />
        </fieldset>
        <fieldset style="border: 1px solid gray; margin-bottom: 10px;">
            <?php foreach($_SESSION['personB'] as $key => $value): ?>
                <input type="checkbox" name="check2[]" value="<?php echo $value; ?>" /> <?php echo $value; ?>
            <?php endforeach; ?>
            <br/>
            <input type="submit" name="submit2" value="Transfer to A" />
        </fieldset>
        <br/>
        <input type="submit" name="reset" value="Reset" />
    </form>
    

    【讨论】:

    • @user2987364 欢迎您,感谢您的支持/接受回答。美好的一天
    最近更新 更多