CodeIgniter Forums
Easy Checkbox Update - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Easy Checkbox Update (/showthread.php?tid=18683)

Pages: 1 2


Easy Checkbox Update - El Forum - 05-14-2009

[eluser]Fielder[/eluser]
I think I'm missing something really simple..

My form successfully works for adding a new user, and when updating a user status FROM 0(inactive) to 1(active). However, it does not work FROM 1 to 0 when editing. It just doesn't change the value to NULL or blank.

View:
Code:
<input type="checkbox" name="user_status" value="1" <?php if ($user_status == '1') { echo " CHECKED"; } ?> />

Database Setup:
Code:
user_status is a tinyint, min length 1, Null Yes, default NULL



Easy Checkbox Update - El Forum - 05-14-2009

[eluser]TheFuzzy0ne[/eluser]
Please post the code you're using for updating the database.


Easy Checkbox Update - El Forum - 05-14-2009

[eluser]dmorin[/eluser]
If a checkbox isn't checked, it isn't actually posted by the form. Your issue is likely that you're not updating the value to 0 because you aren't getting a post value when you should be setting it to 0 any time you don't get a post value.


Easy Checkbox Update - El Forum - 05-14-2009

[eluser]Fielder[/eluser]
My controller:
Code:
$inputSchema = array('user_firstname' => '', 'user_lastname' => '', 'user_phone' => '', 'user_email' => '', 'user_rep' => '', 'user_coord' => '', 'user_status' => '', 'ug_id' => '');
$inputData = array_intersect_key($_POST, $inputSchema);

$this->Users->insertNewUser($inputData);

My model:
Code:
function insertNewUser($inputData)
    {
        $this->db->insert('user', $inputData);
    }


This is the part of my controller loading up the data for the user edit form, but like I mention it already displays the correct "status code" for the user to be edited.
Code:
$user_id = $this->uri->segment(3);
            
                $data['results'] = $this->Users->getUser($user_id);
                //load database query results into array for the input form
                foreach ($data['results'] as $row)
                {
                    $data['user_id'] = $row['user_id'];
                    $data['user_firstname'] = $row['user_firstname'];
                    $data['user_lastname'] = $row['user_lastname'];
                    $data['user_phone'] = $row['user_phone'];
                    $data['user_email'] = $row['user_email'];
                    $data['user_rep'] = $row['user_rep'];
                    $data['user_coord'] = $row['user_coord'];
                    $data['user_status'] = $row['user_status'];
                    $data['ug_id'] = $row['ug_id'];
                }
                
                $data['groupValueName'] = $this->Users->getGroupValue($data['ug_id']);
                
                $this->load->view('include/header.php', $data);
                $this->load->view('user_form');
                $this->load->view('include/footer.php');

I'm going to take a close look at it again.


Easy Checkbox Update - El Forum - 05-14-2009

[eluser]TheFuzzy0ne[/eluser]
I'd like to point out that if someone manipulates your form, it can cause database errors. In you're model, you could add a line like this before the insert statement:

Code:
$inputData['user_status'] = ($inputData['user_status'] == 1) ? 1 : 0;



Easy Checkbox Update - El Forum - 05-14-2009

[eluser]dmorin[/eluser]
Quote:array_intersect_key() returns an array containing all the values of array1 which have matching keys that are present in all the arguments.

Like I said, checkboxes aren't posted if they're unchecked.


Easy Checkbox Update - El Forum - 05-14-2009

[eluser]Dam1an[/eluser]
I always use isset to test if a checkbox is checked.. never let me down thus far


Easy Checkbox Update - El Forum - 05-14-2009

[eluser]Fielder[/eluser]
[quote author="TheFuzzy0ne" date="1242346498"]I'd like to point out that if someone manipulates your form, it can cause database errors. In you're model, you could add a line like this before the insert statement:

Code:
$inputData['user_status'] = ($inputData['user_status'] == 1) ? 1 : 0;
[/quote]

Fuzzy... manipulates it in what way? You mean because of the way I have my form setup in general?


Easy Checkbox Update - El Forum - 05-14-2009

[eluser]Fielder[/eluser]
Ok, so I should assume it is to be set to Zero if empty($inputData['user_status']) is true, and just set it manually. Thanks for the pointers.


Easy Checkbox Update - El Forum - 05-14-2009

[eluser]dmorin[/eluser]
Actually, rather than checking if it's empty, you should use !isset() as I think calling empty on a non-existent index will cause a notice to be generated. But you're correct otherwise. Good luck!