PHP:将选中的元素(通过复选框)从一个数组移动到另一个数组

时间:2014-05-17 13:10:15

标签: php html arrays

<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>

以下是我希望它如何运作:

- 我选择(例如)杰克,戴夫和史密斯,通过检查他们名字的复选框,然后按提交按钮。它们从数组PersonA移动到数组PersonB

-I选择(例如)汤姆通过检查他姓名的复选框,然后按提交按钮。他从数组personB移动到数组personB

所以现在数组看起来像这样: $ personA = array(&#39; Daniel&#39;,&#39; Peter&#39;,#39; Tom&#39;); $ personB = array(&#39; Jack&#39;,&#39; Dave&#39;,&#39; Smith&#39;);

- 该程序允许我继续这样做,直到我按下任一提交按钮而没有选中任何复选框。然后它应该显示两个阵列中的每个人。

1 个答案:

答案 0 :(得分: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>