Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php - inserting the values of multiple checkbox selections

the site i'll be refering to is

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

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?


2 Answers

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:

<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:

#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.

Hope this helps!

like image 63
Stepan Mazurov Avatar answered May 23 '26 03:05

Stepan Mazurov


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.

like image 43
rg88 Avatar answered May 23 '26 01:05

rg88



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!