【问题标题】:how do you reference an array array in php from $_POST?如何从 $_POST 引用 php 中的数组数组?
【发布时间】:2012-07-17 07:27:33
【问题描述】:
<input type="checkbox" id="one" name="form[age[]]" value="1"/><label for="one">0 to 12 months</label> 

使用 POST 提交后,如何在 PHP 中引用 name。我正在尝试收集所有复选框值,然后收集implode age[],但我需要将其放入form[] 以进行表单验证。

当我print_r[$_POST['form']['age']时,显示Notice: Undefined index: age

【问题讨论】:

    标签: php arrays forms post


    【解决方案1】:

    只用key form和form中的key age:

    $age = $_POST['form']['age'];
    $imploded = implode(',', $age);
    

    HTML-Attribute name的值是$_POST中的key,所以form就是$_POST['form']

    编辑:你的名字值的语法错误,改用form[age][]

    【讨论】:

    • 它不工作。在那个 age[] 中只显示最后一个复选框。我输入的name=错误吗?
    【解决方案2】:

    您应该为每个具有年龄值的复选框使用name="form[age][]",然后在将这些值发送到 PHP 之后,它们将作为数组 $_POST['form']['age'] 可用。

    例子:

    <input type="checkbox" name="form[age][]" value="1"/>
    <input type="checkbox" name="form[age][]" value="2"/>
    <input type="checkbox" name="form[age][]" value="3"/>
    

    将在 PHP 中生成一个数组,如:

    $_POST['form']['age'][0]; // = 1
    $_POST['form']['age'][1]; // = 2
    $_POST['form']['age'][2]; // = 3
    

    【讨论】:

      【解决方案3】:

      我想你想要这个:

      <input type="checkbox" id="one" name="form[age][]" value="1"/>
      <input type="checkbox" id="one" name="form[age][]" value="2"/>
      .
      .
      <input type="checkbox" id="one" name="form[age][]" value="12"/>
      
      <?
      $implode = implode(',', $_POST['form']['age']);
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-30
        相关资源
        最近更新 更多