Welcome Guest, Not a member yet? Register   Sign In
How to unserialize an array from POST?
#1

[eluser]condoace[/eluser]
I am trying to grab POST values from array sent via jquery / ajax. I am using serialize in my view and unserialize in my Controller and also doing a foreach loop, the problem is that I get an error message:

Quote:Severity: Notice

Message: unserialize() [function.unserialize]: Error at offset 0 of 1 bytes

Filename: controllers/admin.php

However, when I debug, the model seems to be called from the second foreach in the Controller, however it is using the array index numbers instead of the actual values of the POST array to delete, see below:

Quote:DATABASE: admin QUERIES: 2 (Hide)
0.0004 DELETE FROM admin_users WHERE id ='0'
0.0015 DELETE FROM admin_users WHERE id ='1'

MY VIEW

Code:
$jq.ajax({
    
                    url: '<?php echo base_url();?>index.php/admin/delete_admin_user',
      data: $jq('.sel_emp').serialize(),
     type:"POST",
                    success: function() {
      $jq('#doubletrouble').dialog( 'close' );
      //[removed].reload(true)
                    } //end success
                });
    
.. // other form inputs

form_checkbox(array('name' => 'campaigns[]', 'value' => $row->id, 'class' => 'sel_emp'))

CONTROLLER

Code:
function delete_admin_user() {
  
   $this->output->enable_profiler(true);
  
   $post = $this->input->post('admin_users');
                    
    if(!empty($post)){
    
    $this->load->model('model_admin');
      
      foreach($post as $id => $v){
      
       $data[] =  unserialize($id);    
              
      }
    
      foreach($data as $d =>$k) {
      
      $this->model_admin->delete_admin_user($d);
      
      }

       }
        
  }

#2

[eluser]Aken[/eluser]
In your controller, use print_r($post) to see what the structure of your array is (aka where your IDs are). The way you're looping it, you're pulling the array KEYS not the VALUES. Perhaps your user IDs are the values of the array.
#3

[eluser]condoace[/eluser]
I just did a var_dump and got the following:

Quote:array(3) { [0]=> string(1) "2" [1]=> string(1) "3" [2]=> string(1) "4" }

I think the problem may be with the way I am doing the foreach loop or using the serialize function, not really sure?
#4

[eluser]Aken[/eluser]
If that's your var_dump of $post, you don't need to unserialize anything. It's already being received as an array like it's supposed to. So...
Code:
foreach ($post as $id)
{
$this->model_admin->delete_admin_user($id);
}
#5

[eluser]condoace[/eluser]
Thanks!! that was the problem!




Theme © iAndrew 2016 - Forum software by © MyBB