Welcome Guest, Not a member yet? Register   Sign In
Problem with checkbox fields and hidden fields
#1

[eluser]gbar[/eluser]
Hi, I have a problem sending hidden's field $_POST from a form.

I go straight to the point. In a CRUD, i have a list of records, for each page are displayed 5 records. There is a checkbox for each record, so I can make massive actions.
Within the form, there is also an hidden field, one for every record.

Here the view code:

Code:
<?php echo form_hidden('status[]', $rows->status); ?>

<?php
   $data = array(
     'name'        => 'upd_id[]',
     'value'       => $rows->id_book
   );                                        
?>
<?php echo form_checkbox($data); ?>

If I select one, two, or three check boxes (not all), the values ​​of $ _POST checkbox is sent correctly, but instead those of the hidden field is sent ALL, whether I've selected one, or two or three checkboxes.

For example, in the example below, I selected two checbox, but I see all the hidden fields.

Code:
POST DATA  

$_POST['status']      

Array
(
    [0] => 0
    [1] => 1
    [2] => 1
    [3] => 1
    [4] => 1
)

$_POST['upd_id']      

Array
(
    [0] => 235
    [1] => 234
)

$_POST['submit']       Update

Yet the field is hidden inside a loop, just as the field checkbox.

What's wrong?

Thanks
#2

[eluser]cideveloper[/eluser]
hidden inputs have nothing to do with the checkboxes. Just because you check or uncheck and item doesnt mean the hidden item will stop working or not be registered in the post. you can get the values from the status post array by calling the index of the submitted upd_id post array.

Code:
$status_array = $this->input->post('status');

foreach($this->input->post('upd_id') as $key => $val){
echo $val . ' - ' . $status_array[$key] . '<br />';
}

P.S. I havent tested the code but should give you a start
#3

[eluser]gbar[/eluser]
Hello and thank you for reply to me. Unfortunately your solution, despite having tried in various ways, also gives me a strange behavior.

I try to explain it.

I leave the code of the form:

Code:
&lt;?php echo form_hidden('status[]', $rows->status); ?&gt;
&lt;?php
   $data = array(
     'name'        => 'upd_id[]',
     'value'       => $rows->id_book
   );                                        
?&gt;
&lt;?php echo form_checkbox($data); ?&gt;

This, however, is the part on the controller. I entered the print_r to see what happens.

Code:
// other code.....
            }
            else
            {
                $status = $this->input->post('status');
                
                foreach($this->input->post('upd_id') as $key => $val){
                    echo 'upd_id: ' . $val . ' - status: ' . $status[$key] . '<br />';
                }  
                echo'<br />';
                print_r($upd_id);
                echo'<br />';
                print_r($status);
                //$this->statusBooksBatch($sortBy, $sortOrder, $page, $upd_id, $status);
            }
// other code.....

Now let's take a couple of examples
The scenario is to have 4 records, the first and third with status = 0, the second and fourth with status = 1.
Like this:
Code:
Record id: 242 - status 0
Record id: 241 - status 1
Record id: 240 - status 0
Record id: 239 - status 1
First case, I select all the checkboxes, I get this:
Code:
upd_id: 242 - status: 0
upd_id: 241 - status: 1
upd_id: 240 - status: 0
upd_id: 239 - status: 1

Array ( [0] => 242 [1] => 241 [2] => 240 [3] => 239 )
Array ( [0] => 0 [1] => 1 [2] => 0 [3] => 1 )
correct!

Second case, I select the first two:
Code:
upd_id: 242 - status: 0
upd_id: 241 - status: 1

Array ( [0] => 242 [1] => 241 )
Array ( [0] => 0 [1] => 1 [2] => 0 [3] => 1 )
correct!

But if I select the second and third checkbox, the values ​​are incorrect.
Code:
upd_id: 241 - status: 0
upd_id: 240 - status: 1

Array ( [0] => 241 [1] => 240 )
Array ( [0] => 0 [1] => 1 [2] => 0 [3] => 1 )
Incorrect!

The same if I select only the fourth.
Code:
upd_id: 239 - status: 0

Array ( [0] => 239 )
Array ( [0] => 0 [1] => 1 [2] => 0 [3] => 1 )
Incorrect!

In practice, everything is correct if I select all or some but always selecting the first.
if I select the second checkbox on, the value of status will always start from the first index and everyway, making no more values ​​coincide.

I hope that I explained. Sorry for the length of the post.

Thank you very much!
#4

[eluser]cideveloper[/eluser]
Try this. Very similar to how the cart class deals with stuff.

Controller

Code:
echo '<pre>' . chr(10);
print_r($this->input->post());
echo '</pre>' . chr(10);

View

Code:
&lt;?php $i = 1; ?&gt;
&lt;?php foreach ($row_info as $rows) :?&gt;
&lt;?php echo form_hidden($i.'[status]', $rows->status); ?&gt;
&lt;?php
   $data = array(
     'name'        => $i.'[upd_id]',
     'value'       => $rows->id_book
   );                                        
?&gt;
&lt;?php echo form_checkbox($data); ?&gt; <br />
&lt;?php $i++; ?&gt;
&lt;?php endforeach; ?&gt;
&lt;?=form_submit('mysubmit', 'Submit Item!');?&gt;

results

No Checkbox
Code:
Array
(
    [1] => Array
        (
            [status] => 0
        )

    [2] => Array
        (
            [status] => 1
        )

    [3] => Array
        (
            [status] => 0
        )

    [mysubmit] => Submit Item!
)

second checkbox only

Code:
Array
(
    [1] => Array
        (
            [status] => 0
        )

    [2] => Array
        (
            [status] => 1
            [upd_id] => 241
        )

    [3] => Array
        (
            [status] => 0
        )

    [mysubmit] => Submit Item!
)

etc... As you can see if you check a box you will get a upd_id key in the array
#5

[eluser]gbar[/eluser]
Hello, now the status is correctly associated to upd_id.
But now I'm even more confused ... Meanwhile, for some limits that I have in my knowledge of the arrays, but actually i've the problem of how to build new data upd_id - status in an array, for to pass to the method of the model and run the update query.

Let me explain, before you rename the variables of the form, I put in the controller, the data $_POST in this way:

Code:
$upd_id = $this->input->post('upd_id');
$status = $this->input->post('status');

Now, theoretically it would be easy, I put, in the controller, a code like this:

Code:
$data = $this->input->post();
$status = array();
$upd_id = array();

for($i=1;$i<count($data);$i++)
{
$status[$i] = $this->input->post($i . '[status]');
$upd_id[$i] = $this->input->post($i . '[upd_id]');
}
echo '<pre>' . chr(10);
print_r($status);
echo'<br />';
print_r($upd_id);
echo '</pre>' . chr(10);

But I do not get anything, not even errors for the truth ...

looking at the page's source code, this is the new name of variables:
Code:
&lt;input type="checkbox" name="1[upd_id]" value="242"  /&gt;
&lt;input type="hidden" name="1[status]" value="0" /&gt;

I can not understand, where am I wrong ...

Thank's
#6

[eluser]cideveloper[/eluser]
Code:
for($i=1;$i<count($data);$i++)
{
$status[$i] = $data[$i]['status'];
$upd_id[$i] = (isset($data[$i]['upd_id'])) ? $data[$i]['upd_id'] : null;
}
#7

[eluser]gbar[/eluser]
First of all, a big thanks to the efficiency of your code, and for the patience you've had. The problem has been resolved.

Post here what steps I have done, then if you can kindly tell me if the shape I used to distribute the code, could be improved.

View:
-----------------------------------------
Code:
&lt;?php echo form_hidden($i.'[status]', $rows->status); ?&gt;
&lt;?php
   $data = array(
     'name'        => $i.'[upd_id]',
     'value'       => $rows->id_book
   );                                        
?&gt;
&lt;?php echo form_checkbox($data); ?&gt;

Controller: (Here I get the information from the press of the button status)
------------------------------------------
Code:
}
    else
{                
    $data = $this->input->post();
    $this->statusBooksBatch($sortBy, $sortOrder, $page, $data);
  }
}

and... here I call the method of the model, passing in the array $ data

Code:
.......
function statusBooksBatch($sortBy, $sortOrder, $page, $data)
{
   if($data)
   {
       if($this->books_model->_statusBooksBatch($data))
       {
......

Model:

Code:
function _statusBooksBatch($data)
{                
$status = array();
$upd_id = array();
            
for($i=1;$i<count($data);$i++)
{
  $status[$i] = $data[$i]['status'];
  $upd_id[$i] = (isset($data[$i]['upd_id'])) ? $data[$i]['upd_id'] : NULL;
          
  $status[$i] = $status[$i] == 0 ? 1 : 0;
                    
  $data[$i] = array('status' => $status[$i]);
  $this->db->where('id_book', $upd_id[$i]);
  $query = $this->db->update('books', $data[$i]);                            
}
            
  if($query)
  {
    return TRUE;
  }
    else
  {
    return FALSE;
  }
}

Thank you very much!




Theme © iAndrew 2016 - Forum software by © MyBB