Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send an array via the url in javascript/jQuery

I am trying to send a javascript array via the url.But it fails

function viewReport(mode,someid){
    if(mode==0){
        var para= new Array();
        para['para1'] = 'para1'||0;
        para['para2']= 'para2' || 0;
        console.log(para);
        window.open('somePDFView/'+para,'_blank'); 
    }else{
        var para=[];
        var paraelements={
          para1:'anotherpara1'||0,
          para2:'anotherpara2'||0
        };
        para[0]=paraelements;
        window.open('somePDFView/'+para,'_blank'); 
    }
}

On if part(mode=0), the para array is not sending any more and on else part (mode=1) the para is sends like this:

somePDFView/[object Object]

Which shows the error:

The URI you submitted has disallowed characters

How can we send an array via url.I can't use Ajax (because its a popup window) or session or storing in a temporary table.Also how can we retrieve this value in the controller.

Edit:

I miss an important thing that I am using codeigniter. Then I think it disallows special characters like - &,=,[,],etc..So if any other methods available for sending data as an array?..

like image 336
manuthalasseril Avatar asked Aug 24 '13 09:08

manuthalasseril


2 Answers

To send an array of params via URL from Javascript to PHP/Codeigniter, you need to follow 2 steps:

  1. Convert your params array to JSON String and send that as a single query param.
    para = JSON.stringify(para);
    window.open('somePDFView?params='+para,'_blank');

  2. Decode the query string param (i.e JSON string) at your controller
    $params = json_decode($_GET['params']);

Now you can use all the params at your controller by accessing through $params.

Your code block becomes:

function viewReport(mode,someid){
    if(mode==0){
        var para= new Array();
        para['para1'] = 'para1'||0;
        para['para2']= 'para2' || 0;
        console.log(para);
        para = JSON.stringify(para);
        window.open('somePDFView?params='+para,'_blank'); 
    }else{
        var para=[];
        var paraelements={
          para1:'anotherpara1'||0,
          para2:'anotherpara2'||0
        };
        para[0]=paraelements;
        para = JSON.stringify(para);
        window.open('somePDFView?params='+para,'_blank'); 
    }
}

Try this out and let me know if you still faces this issue.

like image 122
Siva Avatar answered Oct 27 '22 18:10

Siva


You can also use Json here. Use JSON.stringify().

Note: Don't sent long data over a URL. There is a limit for sending data via url and it will end up in a corrupted data if exceeded the limit. For large data, use POST method.

function viewReport(mode,someid){
    var json;
    if(mode==0){
        var para= new Array();
        var paraelements={
      para1:'para1'||0,
      para2:'para2'||0
    };
    para[0]=paraelements;
    json = JSON.stringify(para);
    window.open('somePDFView/'+json,'_blank'); 
    }else{


 var para=[];
    var paraelements={
      para1:'anotherpara1'||0,
      para2:'anotherpara2'||0
    };
    para[0]=paraelements;
    json = JSON.stringify(para);
    window.open('somePDFView/'+json,'_blank'); 
  }
}

If you are using php, use json_decode() to convert json into PHP variable.Also refer, http://php.net/manual/en/function.json-decode.php

like image 45
raduns Avatar answered Oct 27 '22 18:10

raduns