【问题标题】:Passing checkbox array from a Form to PHP variable将复选框数组从表单传递给 PHP 变量
【发布时间】:2017-07-24 05:23:46
【问题描述】:

感谢您查看我的问题。

我正在尝试这样做: 使用带有复选框的表单:用户单击多个复选框(针对一个问题)。 然后点击“提交” 然后一个 PHP 页面获取表单元素并将它们放入变量中。我可以对其他数据执行此操作,但不确定如何使用复选框(因为它是一个数组)。

你能帮帮我吗?

$Q1 = $_POST['Hood'];  // This one is the array. Let's say it's an array that holds 3 words (red, white, blue). I'm fine if they all get stored in $Q1, I just don't know how to loop through the array (in $POST['Hood']) to get all 3 words in that one variable ($Q1)

$Q2 = $_POST['Handle'];
$Q3 = $_POST['Lever'];

所以我想我需要弄清楚如何循环遍历 $_POST['Hood'] 以便从中取出每个数组元素。请帮忙:)

感谢您的帮助!

【问题讨论】:

    标签: php arrays forms checkbox


    【解决方案1】:

    这是表单输入的外观以及 PHP 中的处理部分。注意输入名称有一个数组初始化:

    echo '<form method="post">
    <input type="checkbox" name="Hood[]" value="some value"> some value<br>
    <input type="checkbox" name="Hood[]" value="some other value"> some other value<br>
    <input type="checkbox" name="Hood[]" value="1"> a number<br>
    <button type="submit">Submit</button>
    </form>';
    
    if(isset($_POST['Hood'])) {
    
        foreach($_POST['Hood'] as $key=>$value) {
    
            echo $value;
    
        }
    
    }
    

    【讨论】:

    • 太好了,我现在就去试试!
    猜你喜欢
    • 2013-09-24
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    相关资源
    最近更新 更多