-1

I'm thinking for a couple of days how to best insert multiple checkboxes in my database.

What I want to do is if an user selects an option, associated checkboxes are shown below the select.

For example:

  • User selects option 1 -> Checkbox A, B and C appears
  • User selects option 2 -> Checkbox D, E and F appears

My options come from 1 table and the checkboxes come from another table.

That already is working fine.

But how do I insert the chosen checkboxes (e.g. A and C) in my database that I am able to analyze the data from this specific submit (e.g. User has submitted the form -> Submit 1, Option 1 has been selected and only 75% of checkboxes have been checked; Checked boxes are A and C; Checkbox C was missing)

I hope I could explain it. I want to "simply" submit data to analyze it afterwards.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • It sounds like you have two different questions: 1) how do you show/hide elements based on user action, and 2) how do you actually process the results when the form is submitted. The first is a JavaScript, not php, answer; the second requires that you show some code – Tim Morton Jan 04 '20 at 13:28

3 Answers3

0

use Array for select multiple check box like this

<input type="checkbox" name="Days[]" value="Daily">Daily<br>

Example

VIKAS KATARIYA
  • 5,867
  • 3
  • 17
  • 34
-1

You should probably use two tables:

One with the information about which checkboxes go with which option:

Option | Checkbox
1      | A
1      | B
1      | C
2      | D
2      | E
2      | F

And then a second for value

SubmidId | Option | Checkbox | Value
1000     | 1      | A        | 1
1000     | 1      | B        | 0
1000     | 1      | C        | 1

This will allow you to use the 1st table to define your structure and the second to store your records. Don't hesitate to use the PIVOT command if it makes it easier for you to display stuff.

Bonus: adding a version to both tables could allow you to change the structure and keep historical consistency.

Pierre
  • 159
  • 4
  • The amount of checkboxes and options can vary because it is getting its values from another table where the user can put more values into – Enis Gedikli Jan 04 '20 at 13:21
  • Ok, I'll edit the answer for "undefinied number of checkboxes" – Pierre Jan 04 '20 at 13:22
  • This is still a flat file table, only now you’re duplicating information. What is the need for the first table if its information is wholly repeated in the second? – Tim Morton Jan 04 '20 at 13:42
  • The point of the first table is to have the option/check link configuration. This can change over time. In the second table, you have the values and the version of the option/checkbox link associated. – Pierre Jan 04 '20 at 13:57
-1

There are a couple of approaches to your specific scenario, one of them is what Pierre mentioned. The other one is that what ever values you are assigning to the checkbox element, save all the selected values in a column with comma separated values.

For example: In option 1 you have three checkboxes having values some values,

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

Suppose a user selects 2 checkboxes from option 1 and three from option 2. Now in the request you should have multiple entries in test[] variable like: [1,3,4,5,6]. Save all these entries in one column as varchar:

id | test
1  | 1,3,4,5,6

And at the retrieving time you can explode() them by comma and perform any action you like with respect to each checkbox entry.

This way you can have as many checkbox options as you like saved under one category. I hope this helps, and answer your question.

Saud
  • 859
  • 1
  • 9
  • 23
  • Thought about this solution yesterday but read that if you want to insert information in array form that the database is not being used the right way. – Enis Gedikli Jan 04 '20 at 16:18