Aditx
Aditx

Reputation: 21

passing array javascript to controller codeigniter

i having problem with passing array from view to controller. Here's the case:

VIEW

    <script type="text/javascript"> 
function displayDet(kode,rowNo,rows){
    var jsarr = new Array();
    jsarr = {'kode': kode,'rowno':rowNo,'rows':rows};
    //alert(jsarr['kode']+jsarr['rowno']+jsarr['rows']);
    window.location.href='http://localhost/ci_hiandgirls/index.php/sales/sales/form_so_arr/'+jsarr;  
}
</script>

CONTROLLER

public function form_so_arr( $params = array() ){
    foreach($params as $val){
        $view['detRows'] = $val['rows'];
        $view['kode'] = $val['kode'];
        $view['rowNo'] = $val['rowNo'];
    }       
    $this->load->view('sales/form',$view);
}

Is this right ? it's show error message "Invalid argument supplied for foreach()". I just want to catch array from javascript then send back the values to VIEW. kindly please help me.

Upvotes: 1

Views: 2936

Answers (3)

Pattle
Pattle

Reputation: 6014

This seems like bad practice. But if you want to do it like this pass the parameters as different url segments like

<script type="text/javascript"> 
function displayDet(kode,rowNo,rows){
window.location.href='http://localhost/ci_hiandgirls/index.php/sales/sales/form_so_arr/'+kode+'/'+rowNo+'/'+rows;  
}
</script>

Also you shouldn't be hard coding your base URL, you want to do this dynamically because what happens if you move your site and the base URL changes.

Upvotes: 1

gen_Eric
gen_Eric

Reputation: 227310

You cannot shouldn't pass an array like that. You should be using GET/POST, not the URI segments for this.

NOTE: You might need to edit the CodeIgniter config file to enable GET.

public function form_so_arr(){
    $params = $this->input->get('params');
    foreach($params as $val){
        $view['detRows'] = $val['rows'];
        $view['kode'] = $val['kode'];
        $view['rowNo'] = $val['rowNo'];
    }       
    $this->load->view('sales/form',$view);
}

Then in your JavaScript:

// From http://stackoverflow.com/a/1714899
function serialize(obj, prefix) {
    var str = [];
    for(var p in obj) {
        var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
        str.push(typeof v == "object" ? 
            serialize(v, k) :
            encodeURIComponent(k) + "=" + encodeURIComponent(v));
    }
    return str.join("&");
}

function displayDet(kode,rowNo,rows){
    var jsarr = {'kode': kode,'rowno':rowNo,'rows':rows};
    window.location.href='http://localhost/ci_hiandgirls/index.php/sales/sales/form_so_arr?'+serialize(jsarr,'params');
}

If you can't use the above method, you can still pass the array in the URI segment, just as a JSON string.

function displayDet(kode,rowNo,rows){
    var jsarr = {'kode': kode,'rowno':rowNo,'rows':rows};
    window.location.href='http://localhost/ci_hiandgirls/index.php/sales/sales/form_so_arr/'+encodeURIComponent(JSON.stringify(jsarr));
}

Then in PHP:

public function form_so_arr($params='[]'){
    $params = json_decode(urldecode($params), TRUE);
    foreach($params as $val){
        $view['detRows'] = $val['rows'];
        $view['kode'] = $val['kode'];
        $view['rowNo'] = $val['rowNo'];
    }       
    $this->load->view('sales/form',$view);
}

Upvotes: 0

M Khalid Junaid
M Khalid Junaid

Reputation: 64496

It is a bad practice to send the array in url CI provides the uri segment to pass and get parameters from the url you can do it in this way also

<script type="text/javascript"> 
function displayDet(kode,rowNo,rows){
window.location.href='http://localhost/ci_hiandgirls/index.php/sales/sales/form_so_arr/'+kode+'/'+rowNo+'/'+rows;  
}
</script>

Now in your controller you can get these params as

public function form_so_arr(){
        $view['detRows'] = $this->uri->segment(6);
        $view['kode'] = $this->uri->segment(4);
        $view['rowNo'] = $this->uri->segment(5);

    $this->load->view('sales/form',$view);
}

You should read the Uri Segment Working

Here is a little overview of uri segment that how it works

http://example.com/index.php/news/local/metro/crime_is_up
The segment numbers would be this:

uri segment (1) =>news
uri segment (2) =>local
uri segment (3) =>metro
uri segment (4) =>crime_is_up

Hope it makes sense

Upvotes: 0

Related Questions