Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
1  
var jsarr = new Array(); jsarr = {'kode': kode,'rowno':rowNo,'rows':rows}; This is not array, this is object. Simply do var jsarr = {'kode': kode,'rowno':rowNo,'rows':rows}; –  jQuery00 Jul 5 '13 at 9:21
    
i've tried it, still same error –  Aditx Jul 5 '13 at 9:59
    
How do you get the json object from your url in the controller? I think you would have to json_decode it before you can loop through it with a foreach in php. –  Jeemusu Jul 5 '13 at 10:18

3 Answers 3

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.

share|improve this answer

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

share|improve this answer
    
thanks for answer. But i mean if one of segment is array how to catch it in controller function ? I tried to sent array from View to Controller. Then i set "function form_so_arr($params = array())" Is this correct ? –  Aditx Jul 5 '13 at 10:50

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);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.