Jump to content

when user clicks checkbox, it's not captured in php array


johnmerlino

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.