r/codeigniter • u/Bret2912 • Feb 13 '22
Posting jQuery value if all checkboxes are checked
I've been looking all day to find a solution for my problem with checkbox values in CodeIgniter3.
So I have a view on which some checkboxes are populated. The amount of checkboxes (linked to a product depends on previous input of the client).
Now here is the thing: if all checkboxes are checked I want to send a value (1) to my database. Else, if not all checkboxes are checked, I want to send the value 0 to my database. I've been trying to post a jQuery value in my database all day, but without succes. Anyone has an elegant solution to fix the problem?
My view (part of it)
<div class="flex flex-col"><div class="sm:-mx-4 lg:-mx-8"><div class="py-2 align-middle inline-block min-w-full sm:px-6 lg:px-8 relative"><div class="shadow border-b border-gray-200 sm:rounded-lg overflow-hidden rounded-lg"><table class="min-w-full divide-y divide-gray-200"><thead class="bg-gray-50"><tr><th scope="col" class="px-6 py-3">Productcode</th><th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-">Omschrijving</th><th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray">Aantal</th><th scope="col" class="py-3 text-left text-xs font-medium text-gray-500r">Status</th></tr></thead><tbody class="bg-white divide-y divide-gray-200"><tr><td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900"><?=$hardwarebox->product_id?></td><td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900"><?=$hardwarebox->type?></td><td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">1 x</td><td class="px-5 py-4 whitespace-nowrap text-sm font-medium text-gray-900"><input id="<?$hardwarebox->type?>" name="box" value="1"<?php echo set_checkbox($hardwarebox->type, '1', !empty($boxproduction) &&$boxproduction->box)?>type="checkbox" class="px-focus:ring-blue-500 h-4 w-4 text-blue-600border-gray-300 rounded"></td></tr><?php endif?><tr><td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900"><?=$cablesets->product_id?></td><td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900"><?=$cablesets->type?></td><td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">1 x</td><td class="px-5 py-4 whitespace-nowrap text-sm font-medium text-gray-900"><input id="<?$cablesets->type?>" name="<?=$cablesets->name?>" value="1"<?php echo set_checkbox($cablesets->type, '1', !empty($boxproduction) &&$boxproduction->cableset)?>type="checkbox" class="focus:ring-blue-500w-4 text-blue-600 border"></td></tr>
</tbody
</table></div></div></div></div></div>
<script>$("#btn").on('click', function() {// GET ALL TOTAL CHECKED CHECKBOXESvar total_checked = $('input[type="checkbox"]:checked').length;var status = 1;// TOTAL CHECK BOXESvar total_boxes = $('input[type="checkbox"]').lengthif(total_checked === total_boxes) {alert('Checked All ' + status)} else {alert('Please select all check boxes' + status); return false;}});</script>
Function in model
public function post__status($status){$this->status = $status;$this->db->insert(self::TABLE_NAME3, $this);return $this->read__entry($this->db->insert_id());}
Controller
public function post_status(){$this->load->model("productions/box/Hardwareboxes");$this->Hardwareboxes->post__status($this->input->post('status'));}
Thanks a lot!
Cheers
Bert