• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trying to updating multiple records with checkboxes but nothing updates

#1
[eluser]johnmerlino[/eluser]
Hey all,

have a list of records that get rendered in two different tables. Html looks like this:

Code:
<div id="contents">
        &lt;form action="http://site/blogs/admin/urls/update" method="post"&gt;
            <table>
                <thead>
                    <tr><th>url</th><th>approved</th><th>approve?</th></tr>
                </thead>
                <tbody>
                    <tr><td>michelleryan</td><td>true</td><td>&lt;input type="checkbox" name="check[1]" value="approve?" checked="checked"  /&gt;&lt;/td></tr>
                </tbody>
            </table>
            <table>
                <thead>
                    <tr><th>url</th><th>approved</th><th>approve?</th></tr>
                </thead>
                <tbody>
                    <tr><td>johnmerlino</td><td>false</td><td>&lt;input type="checkbox" name="check[3]" value="approve?"  /&gt;&lt;/td></tr>
                </tbody>
            </table>
            &lt;input type="submit" name="submit" value="Update"  /&gt;
        &lt;/form&gt;
    </div>

When I call the update method, nothing updates:

Code:
function update(){
            $vanity_url = new VanityUrl();
            $checkbox = $this->input->post('check');
            if(isset($checkbox)){
                foreach($checkbox as $key => $value){
                    $vanity_url->where('id',$key)->get();
                    $vanity_url->save();
                }
            }
        }

What I think the problem is that while I am updating the record, I am not updating it with any changed value. I think I need to do something like this to determine if the record field 'approved' has been checked or not:

Code:
$vanity_url->approved = ;

I just don't know what value to give it.

Thanks for response.

#2
[eluser]InsiteFX[/eluser]
Code:
checked   = TRUE;
unchecked = FALSE;

InsiteFX

#3
[eluser]johnmerlino[/eluser]
Thanks for response. THe problem is that even when I check the checbox, it still sends a value of 0 to the php array and hence nothing is ever updated:

Code:
form_checkbox("approved[$row->id]",$row->approved, $row->approved)
When using $this->input->post('approved'), I get this array:

Code:
array(2) { [1]=> string(1) "1" [3]=> string(1) "0" }
Notice the string "0" at the end. That should be "1" because I checked that box.

So when it's sent to update:

Code:
function update(){
            $vanity_url = new VanityUrl();
            $checkbox = $this->input->post('approved');
            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:
Code:
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.

#4
[eluser]johnmerlino[/eluser]
When you default the value attribute to 1:

form_checkbox("approved[$row->id]",'1', $row->approved)

and then convert it to integer in php, then when user selects checkbox, that value attribute will get passed to php array and it will work. The second argument of form_checkbox gets put in the html value attribute.

#5
[eluser]umefarooq[/eluser]
well i have another solution if you like it will help you more fast, right now its updating all you records for approved and unapproved. here is my solution

just pass id to checkbox array value not as Associative array here is the code for solution

HTML
Code:
&lt;form action="blog/admin/action"&gt;
     &lt;input type="checkbox" name="approved[]" value="1" /&gt;
     &lt;input type="checkbox" name="approved[]" value="2" /&gt;
     &lt;input type="checkbox" name="approved[]" value="3" /&gt;
     &lt;input type="submit" name="submit" value="approve" /&gt;
     &lt;input type="submit" name="submit" value="unapprove" /&gt;
     &lt;/form&gt;

controller
Code:
function action(){
    $task = $this->input->get_post('submit');
    switch($task){
     case 'approve':
       $this->approve(1);
     break;
     case 'unapprove':
        $this->approve(0);
     break;
   }
}

  function approve($approve = 1){
    $ids = $this->input->get_post(approved);
    if(!empty($ids)){
         foreach($ids as $id){
          $vanity_url->where('id',$id)->get();
          $vanity_url->approved = $approve;
          $vanity_url->save();
      }
    }
     redirect('anywhere');
  }

you can see i have use submit button with same name but value is different it will submit value of that button which we press, and always get the ids of checked boxes and update only specific record not the whole list of records


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.