php - 插入多个复选框选择的值

时间:2022-12-02 17:42:05

the site i'll be refering to is

我将参考的网站是

http://www.iaddesignandstudio.com/offline select the quote tab

http://www.iaddesignandstudio.com/offline选择引用标签

if a person where to fill out this form and select more than one checkbox in either number 1 or number 3. how would i insert those selected values into a database so that when i retreive the information the user inputed or selected i can see which checkboxes he/she selected?

如果一个人在哪里填写这个表格,并在数字1或数字3中选择多个复选框。我将如何将这些选定的值插入数据库,以便当我检索用户输入或选择的信息时,我可以看到哪个复选框他/她选择了吗?

3 个解决方案

#1


4  

You can set your checkboxes to be in one array after the form is submitted by adding [] at the end of the name attribute like such:

通过在name属性的末尾添加[],您可以在提交表单后将复选框设置为一个数组,如下所示:

<input type="checkbox" name="services[]" value="Podcasting Services" /> 
Podcasting Services      
<input type="checkbox" name="services[]" value="Local Search" />Local Search

When the form is submitted, your $_POST['services'] variable will be an array containing all checked values. ex:

提交表单时,$ _POST ['services']变量将是一个包含所有已检查值的数组。例如:

#var_dump($_POST['services']);
array(2) {
  [0]=>
  string(19) "Podcasting Services"
  [1]=>
  string(12) "Local Search"
}

Then you can do anything you want with that, wether it be sending an email or adding fields to the database using ether foreach() or implode() to loop through the values.

然后你可以做任何你想做的事情,无论是发送电子邮件还是使用ether foreach()或implode()在数据库中添加字段来循环遍历这些值。

Hope this helps!

希望这可以帮助!

#2


2  

I would have separate fields in the database for them. Like in question 1, the posted form might submit $_POST['admycomp'] and $_POST['provideresource'] as having been checked. If so, insert a 1 into the database field admycomp or provideresource. That way you have recorded which fields they checked.

我会在数据库中为它们分别创建字段。与问题1一样,发布的表单可能会提交$ _POST ['admycomp']和$ _POST ['provideresource']。如果是这样,请在数据库字段admycomp或provideresource中插入1。这样您就可以记录他们检查的字段。

#3


0  

I would suggest storing the values in your database would be to use the SET datatype (eg: http://dev.mysql.com/doc/refman/5.0/en/set.html). This would store each of the checkbox values from a checkbox group in a single column in a storage-efficient way. You would probably need to use implode as mentioned above to create an SQL query to insert your set.

我建议在数据库中存储值将使用SET数据类型(例如:http://dev.mysql.com/doc/refman/5.0/en/set.html)。这将以存储效率的方式将复选框中的每个复选框值存储在单个列中。您可能需要使用上面提到的implode来创建SQL查询来插入您的集合。

#1


4  

You can set your checkboxes to be in one array after the form is submitted by adding [] at the end of the name attribute like such:

通过在name属性的末尾添加[],您可以在提交表单后将复选框设置为一个数组,如下所示:

<input type="checkbox" name="services[]" value="Podcasting Services" /> 
Podcasting Services      
<input type="checkbox" name="services[]" value="Local Search" />Local Search

When the form is submitted, your $_POST['services'] variable will be an array containing all checked values. ex:

提交表单时,$ _POST ['services']变量将是一个包含所有已检查值的数组。例如:

#var_dump($_POST['services']);
array(2) {
  [0]=>
  string(19) "Podcasting Services"
  [1]=>
  string(12) "Local Search"
}

Then you can do anything you want with that, wether it be sending an email or adding fields to the database using ether foreach() or implode() to loop through the values.

然后你可以做任何你想做的事情,无论是发送电子邮件还是使用ether foreach()或implode()在数据库中添加字段来循环遍历这些值。

Hope this helps!

希望这可以帮助!

#2


2  

I would have separate fields in the database for them. Like in question 1, the posted form might submit $_POST['admycomp'] and $_POST['provideresource'] as having been checked. If so, insert a 1 into the database field admycomp or provideresource. That way you have recorded which fields they checked.

我会在数据库中为它们分别创建字段。与问题1一样,发布的表单可能会提交$ _POST ['admycomp']和$ _POST ['provideresource']。如果是这样,请在数据库字段admycomp或provideresource中插入1。这样您就可以记录他们检查的字段。

#3


0  

I would suggest storing the values in your database would be to use the SET datatype (eg: http://dev.mysql.com/doc/refman/5.0/en/set.html). This would store each of the checkbox values from a checkbox group in a single column in a storage-efficient way. You would probably need to use implode as mentioned above to create an SQL query to insert your set.

我建议在数据库中存储值将使用SET数据类型(例如:http://dev.mysql.com/doc/refman/5.0/en/set.html)。这将以存储效率的方式将复选框中的每个复选框值存储在单个列中。您可能需要使用上面提到的implode来创建SQL查询来插入您的集合。