0

I have quite a large number of checkboxes that I need to get the value of when checked. I would like to save them in a text field with id numbers and divider of ':'

Its hard to explain but I will show the code first and then explain how I would like it,

  <input name="types" type="text" id="types" value="">
  <input name="class" type="text" id="class" value="">

 <input type='checkbox' data-toggle='collapse' id = "1" data-target='#collapsediv1'> Coach
 </input></li>

<div id='collapsediv1' class='collapse div1'>
<li class="coach"><input type="checkbox" value="1"> Economy</li>
<li class="coach"><input type="checkbox" value="2"> Standard</li>
<li class="coach"><input type="checkbox" value="3"> Executive</li>
<li class="coach"><input type="checkbox" value="4"> Premium</li>
</div></li>

 <input type='checkbox' data-toggle='collapse' id="2" data-target='#collapsediv1'> Coach
 </input></li>

<div id='collapsediv1' class='collapse div1'>
<li class="coach"><input type="checkbox" value="5"> Economy</li>
<li class="coach"><input type="checkbox" value="6"> Standard</li>
<li class="coach"><input type="checkbox" value="7"> Executive</li>
<li class="coach"><input type="checkbox" value="8"> Premium</li>
</div></li>

So I have a checkbox id 1 that when clicked collapses a div with another 4 checkboxes. Firstly when the Coach or ID 1 is checked I would like to add :1: to types value and if any of the checkboxes in there are clicked add the values like :1:3: etc to the classes value I can add them but I cant seem to delete them when unchecked? Also I can only do this with seperate functions but it must e possile with only a couple?

Any help of advice would be great, thank you

wayneuk2
  • 156
  • 1
  • 10
  • Why must it be a string, vs some sort of array or object to keep track? – Taplar Aug 17 '18 at 22:48
  • because is sends that value to a database like :1:2:3:4: and I can then get what vehicles and classes the user has from these strings – wayneuk2 Aug 17 '18 at 22:51
  • 1
    You could always build that string when you need it as a string. And keep it as something more editable until then. – Taplar Aug 17 '18 at 22:52
  • ok, im listening. Pretty new to this and thought this is easiest way so where do I start? – wayneuk2 Aug 17 '18 at 22:54
  • 1
    What backend do you use? Most backends support getting the values as an array on submit. Simply give all the checkboxes the same name to group them – NielsNet Aug 17 '18 at 23:31
  • mysql, and not quite sure what you mean? – wayneuk2 Aug 17 '18 at 23:40
  • NielsNet means what kind of a server setup is receiving the form submission/ajax request. PHP, Ruby-on-Rails, Django, etc. Depending on the language on the backend, if you name your checkboxes like this `name="someName[]"` and make that the name for all of the checkboxes, when the form is submitted, the values will aggregate into an array on the server, which you can then parse for your values. – Jocko Aug 18 '18 at 00:38
  • Just standard PHP! I could probably do that. – wayneuk2 Aug 18 '18 at 00:40
  • Possible duplicate of [PHP Multiple Checkbox Array](https://stackoverflow.com/questions/14026361/php-multiple-checkbox-array) – Louys Patrice Bessette Aug 18 '18 at 01:49

0 Answers0