johnmerlino Posted April 5, 2011 Share Posted April 5, 2011 Hey all, THe problem I am having is even when I check the checbox, it still sends a value of 0 to the php array and hence nothing is ever updated: <input type="checkbox" name="approved[1]" value="1" checked="checked" /> <input type="checkbox" name="approved[3]" value="0" /> <input type="checkbox" name="approved[4]" value="0" /> So the second item is not checked by default. But even when I check it, my post array contains this: array(2) { [1]=> string(1) "1" [3]=> string(1) "0" } Notice the "0". It should be "1" now since I checked the second option. This is the function: function update(){ $vanity_url = new VanityUrl(); $checkbox = $this->input->post('approved'); //this is same thing as $_POST if(isset($checkbox)){ foreach($checkbox as $key => $value){ $vanity_url->where('id',$key)->get(); $vanity_url->approved = (int)$value; $vanity_url->save(); $vanity_url->check_last_query(); } } } That check_last_query() function outputs this: UPDATE `vanity_urls` SET `approved` = 1 WHERE `id` = 1 UPDATE `vanity_urls` SET `approved` = 0 WHERE `id` = 3 So despite checking the second option it still sets it to 0. Thanks for response. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted April 5, 2011 Share Posted April 5, 2011 It contains the value you assigned to it, which is 0. Checkboxes that are checked are passed in the $_POST array with the value in the value= attribute. Checkboxes that are unchecked are not passed at all. Quote Link to comment Share on other sites More sharing options...
johnmerlino Posted April 5, 2011 Author Share Posted April 5, 2011 So should I default all the value attributes to 1. Therefore, when the checkbox is checked, it will send that 1 to the php array? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted April 5, 2011 Share Posted April 5, 2011 It appears you're using the primary key id as the value of the checkbox's aray id, if that's correct then I'd say yes. Or you could do it the other way around, and use the PK id as the value= attribute and possibly simplify things a bit by not needing to extract the array keys to get the record's id. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 5, 2011 Share Posted April 5, 2011 EDIT: Pikachu2000 beat me to it, but I'll post this anyway since I provided some sample code So should I default all the value attributes to 1. Therefore, when the checkbox is checked, it will send that 1 to the php array? If you want a 1 to be passed, then yes. As Pikachu2000 stated only checkboxes that are checked are passed in the post data. However, looking at your code, I think I see what you are trying to do and will propose an alternative. It looks like you are naming the checkboxes as an array with the index being an ID value of some sort. You are then checking the POST data and finding which "approved[id]" are checked. There is an easier way. It seems all you really need to know is what IDs were checked. Just create your checkbox array names with NO ID and use the ID as the value. <input type="checkbox" name="approved[]" value="1" checked="checked" /> <input type="checkbox" name="approved[]" value="3" /> <input type="checkbox" name="approved[]" value="4" /> Then in your post data, $_POST['approved'] will contain an array of all the record IDs that were checked. Quote Link to comment Share on other sites More sharing options...
johnmerlino Posted April 5, 2011 Author Share Posted April 5, 2011 Thanks for all these great responses. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.