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

28,002

Solution 1

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.

Solution 2

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

Solution 3

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

Share:
28,002
manuthalasseril
Author by

manuthalasseril

Updated on April 30, 2020

Comments

  • manuthalasseril
    manuthalasseril about 4 years

    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
    manuthalasseril over 10 years
    Its also showing the error --The URI you submitted has disallowed characters.
  • raduns
    raduns over 10 years
    Use console.log(json) and check what data is being return before sending through url.
  • manuthalasseril
    manuthalasseril over 10 years
    the data is correctly assigned and json contains all that values but it can't send
  • raduns
    raduns over 10 years
    Are you sending the data by using query string?
  • manuthalasseril
    manuthalasseril over 10 years
    Sorry I miss a important thing.I am using codeigniter, so i think it does not allows special characters like &,=,[,].Is it right?
  • raduns
    raduns over 10 years
    And make sure that the popup window is not blocked in your browser you used.
  • manuthalasseril
    manuthalasseril over 10 years
    popup window is not blocked....how can we send data as an array in jquery when using codeigniter?
  • raduns
    raduns over 10 years
  • manuthalasseril
    manuthalasseril over 10 years
    Sorry that one uses ajax.I am very thankful for your kind replays.Anyway I will not convert it into an array ,instead sending one by one or using a temporary table.But what the team lead will say .:(
  • Brewal
    Brewal over 10 years
    You can't do this. See this tread or this one.