【问题标题】:post array keep only 1 value后数组只保留 1 个值
【发布时间】:2014-01-03 22:11:55
【问题描述】:

PHP 表单代码:

    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 只保留最后一个值。如何发送我的所有值?

【问题讨论】:

  • 显示您的完整表单代码,以便我们届时为您提供帮助。
  • 也许您应该在表单复选框名称中使用 artwork[] 而不是 artwork
  • 使用isset() insted of != null 并从$rest1[],$rest2[] 中删除[]
  • 我试过艺术品[]没用
  • @MarkBaker:猜对了,但不会导致artwork2 then

标签: php arrays forms post


【解决方案1】:

你可能想编码

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

而不是

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

【讨论】:

  • 希望这是 OP 想要的,不是很清楚他的问题 ;)
【解决方案2】:

对于一个结果,break 应该可以解决问题:

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

【讨论】:

    【解决方案3】:

    原因很简单:如果选中多个同名复选框,你必须为复选框名称指定一个数组,这样你就可以遍历发布的值

    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
    

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

    【讨论】:

    • 抱歉html输出没有写在第一篇文章中
    猜你喜欢
    • 2022-12-05
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 2010-11-20
    相关资源
    最近更新 更多