PHP:如何在变量中从$_POST保存多个复选框数组

时间:2021-10-24 13:08:02

I am new to PHP and am looking for some help with the following:

我是PHP新手,我正在寻找一些帮助如下:

I have a large HTML form with various inputs, selects and textareas. On submit I pass all field values to another PHP page to create an email with them.

我有一个包含各种输入、选择和文本区域的大型HTML表单。在提交时,我将所有字段值传递给另一个PHP页面,以便使用它们创建电子邮件。

This works as intended for text inputs, radio buttons, selects, textareas etc. but not for checkboxes.

这适用于文本输入、单选按钮、选择器、文本区域等,但不适用于复选框。

In the form I have multiple checkboxes that all look as follows using the same name:

在表单中,我有多个复选框,它们都使用相同的名称:

<input type='checkbox' class='someClass' id='language1' name='language[]' value='de - German' />
<input type='checkbox' class='someClass' id='language2' name='language[]' value='en - English' />
<input type='checkbox' class='someClass' id='language3' name='language[]' value='fr - French' />
<!-- ... -->

On the PHP side I tried the following but if I then use $languages for my email body it shows as blank (while all other fields appear correctly):

在PHP方面,我尝试了以下操作,但是如果我对我的电子邮件主体使用$languages,它将显示为空(而所有其他字段都显示正确):

$languages = implode(',', $_POST['language[]']);

What I am trying to do is to save all values from the $_POST["language"] array in one variable, separated with commas (and no comma at the end).

我要做的是将$_POST["language"]数组中的所有值保存在一个变量中,并用逗号分隔(末尾没有逗号)。

Can someone help me with this ?

有人能帮我一下吗?

Many thanks in advance

提前感谢

2 个解决方案

#1


4  

If you use anyname[] in the form, PHP will translate it to an array, but without the [] in the name. So this should work:

如果在表单中使用anyname[], PHP将把它转换为数组,但是在名称中不使用[]。这应该工作:

$languages = implode(',', $_POST['language']);

Note that unchecked checkboxes are not posted, so if you check none, no value is posted and $_POST['language'] will not be set. You would have to check for that.

注意,未选中的复选框没有被张贴,因此如果选中none,则没有张贴任何值,也不会设置$_POST['language']。

$languages = 'No languages selected';
if (isset($_POST['language'])){
  $languages = implode(',', $_POST['language']);
}

#2


0  

HTML

HTML

<input type='checkbox' class='someClass' id='language1' name='language[]' value='1' />
<input type='checkbox' class='someClass' id='language2' name='language[]' value='2' />
<input type='checkbox' class='someClass' id='language3' name='language[]' value='3' />
<!-- ... -->

PHP

PHP

$language_ = array(1=>'de - German',2=>'en - English',3=>'fr - French');
if (!empty($_GET['language'])) {
     foreach ($_GET['language'] as $l) {
                $lan0 .= $language_[$l].', ';
            }
echo rtrim($lan0,", ");

#1


4  

If you use anyname[] in the form, PHP will translate it to an array, but without the [] in the name. So this should work:

如果在表单中使用anyname[], PHP将把它转换为数组,但是在名称中不使用[]。这应该工作:

$languages = implode(',', $_POST['language']);

Note that unchecked checkboxes are not posted, so if you check none, no value is posted and $_POST['language'] will not be set. You would have to check for that.

注意,未选中的复选框没有被张贴,因此如果选中none,则没有张贴任何值,也不会设置$_POST['language']。

$languages = 'No languages selected';
if (isset($_POST['language'])){
  $languages = implode(',', $_POST['language']);
}

#2


0  

HTML

HTML

<input type='checkbox' class='someClass' id='language1' name='language[]' value='1' />
<input type='checkbox' class='someClass' id='language2' name='language[]' value='2' />
<input type='checkbox' class='someClass' id='language3' name='language[]' value='3' />
<!-- ... -->

PHP

PHP

$language_ = array(1=>'de - German',2=>'en - English',3=>'fr - French');
if (!empty($_GET['language'])) {
     foreach ($_GET['language'] as $l) {
                $lan0 .= $language_[$l].', ';
            }
echo rtrim($lan0,", ");