5

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?..

manuthalasseril
  • 1,054
  • 7
  • 17
  • 33

4 Answers4

9

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.

Siva
  • 1,123
  • 2
  • 14
  • 25
6

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

raduns
  • 765
  • 7
  • 18
3

You can try to pass them as individual parameters:

var para1 = '1';
var para2 = '2';
var para= new Array();
para.push('para[]=' + para1 || 0);
para.push('para[]=' + para2 || 0);
var url = para.join('&');
alert(url)

Returns para[]=1&para[]=2. Note that this solution does not require jQuery.

  • Now i can see the parameters on url--salesexePDFView/parat[] = 1&parat[] = 2 but it shows The URI you submitted has disallowed characters. – manuthalasseril Aug 24 '13 at 10:24
  • I removed the spaces between `parat[]` and the `=` sign. It should work now. –  Aug 24 '13 at 10:27
2

Use jQuery.param() and change your data structure :

var para = {
    para1:'anotherpara1'||0,
    para2:'anotherpara2'||0
};

window.open('somePDFView/'+jQuery.param(para), '_blank'); 

The Demo jsFiddle

Brewal
  • 8,067
  • 2
  • 24
  • 37
  • You can't do this. See this [tread](http://stackoverflow.com/questions/4907843/open-url-in-new-tab-using-javascript) or [this one](http://stackoverflow.com/questions/427479/programmatically-open-new-pages-on-tabs). – Brewal Aug 25 '13 at 14:04