post数组只保留1个值

时间:2014-01-03 22:11:55

标签: php arrays forms post

PHP FORM CODE:

    echo '<form name="planeSeat" action="koltuk-sec-process.php" method="post" >';
...
    echo '<input type="checkbox" class="css-checkbox" id="'.$rightplace.'" name="artwork" value="'.$rightplace.'"  />';
...
    echo '<input type="submit" name="formSubmit" value="Üye girişi yapmadan devam et"/>';

PHP流程代码:

    $rest1 = array();
    $rest2 = array();

$rest1[] = htmlspecialchars($_POST["artwork"]);
$rest2[] = htmlspecialchars($_POST["artwork2"]);

   if($rest1 != null){

    print_r ($rest1);

    }

    if($rest2 != null){

    print_r ($rest2);

    }

当我发送图稿时,如果我选中了2个复选框,$rest1仅保留最后一个值。我怎样才能发送我的所有价值观?

3 个答案:

答案 0 :(得分:1)

你可能想要编码

$rest1[] = htmlspecialchars($_POST["artwork"]);
$rest1[] = htmlspecialchars($_POST["artwork2"]);

而不是

$rest1[] = htmlspecialchars($_POST["artwork"]);
$rest2[] = htmlspecialchars($_POST["artwork2"]);

答案 1 :(得分:0)

对于一个结果,break应该做到这一点:

$foobar = array('uno', 'dos', 'tres');
foreach ($foobar as $fb) {
    echo '<p>'.$fb.'</p>';
    break;
}

答案 2 :(得分:-1)

原因很简单:如果检查了多个具有相同名称的复选框,则必须为复选框名称指定一个数组,这样您就可以遍历已发布的值

echo '<input type="checkbox" class="css-checkbox" id="'.$rightplace.'" name="artwork[]" value="'.$rightplace.'"  />';

echo '<input type="checkbox" class="css-checkbox" id="'.$rightplace.'" name="artwork[]" value="'.$rightplace.'"  />'; // second checkbox



print_r ( $_POST['artwork'] ) // will display the artwork array

具有相似内容的所有复选框应具有相同的名称!!!!!