Skip to content

Instantly share code, notes, and snippets.

@pporlan

pporlan/README.md

Forked from mikedfunk/README.md
Last active May 15, 2018
Embed
What would you like to do?

Checkbox Helper

Just a tiny helper to help with checkboxes. When you uncheck a checkbox, it's value does not get sent in the form values. Instead of telling CodeIgniter you want to set this value to 0, it says "don't change it." This fixes that, one by one, by setting the value of the associated $_POST array item to zero. You can also pass in an array of checkbox names and it will do each one.

Usage

$this->load->helper('checkbox');

// set post values to zero for unchecked boxes
fix_unchecked(['is_active', 'is_enabled', 'is_verified']);
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* @author Mike Funk
* @modified_by Pedro Porlan
* @email info@pporlan.com
* @file checkbox_helper.php
*/
/**
* checkbox value fixer. If a checkbox is not checked, sets it's value
* to zero.
* @param mixed $name the checkbox form field name. Can be passed as a
* string or as an array.
* @return void
*/
function fix_unchecked($name) {
$CI = & get_instance();
if ($CI->input->post()) {
if (!is_array($name)) { $name = array($name); }
foreach ($name as $item) {
if (!$CI->input->post($item)) { $_POST[$item] = 0; }
}
}
}
/* End of file checkbox_helper.php */
/* Location: ./application/helpers/checkbox_helper.php */
@ilemonajames

This comment has been minimized.

Copy link

@ilemonajames ilemonajames commented Aug 18, 2017

How do I make use of this helper in my code?
I have an array of products which I can attach to my products, I want to make use of it to return all the unchecked value to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.