0

I am trying to pass an array in a url so I tried this:

?question_id=10&value=1&array=["Saab","Volvo","BMW"]

This didn't work (didn't think it would, but it's a start).

It's a key and value array anyway so I needed something like this:

&array[28]=1&array[9]=1&array[2]=0&array[28]=0

But that didn't work either

2
  • Use post to pass array Commented Mar 15, 2013 at 10:09
  • Passing large arrays via GET can be troublesome as not all browsers support large GET requests. I think GET URLS are limited to 255 bytes, some browsers support larger ones, but the RFC's define it as 255. Commented Mar 15, 2013 at 10:14

4 Answers 4

2

in jquery try this

var arr = [1, 4, 9];
var url = '/page.php?arr=' + JSON.stringify(arr);
window.location.href = url;
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to pass an array, you just have to set the different parts of the array in the URI like

http://example.com/myFile.php?cars[]=Saab&cars[]=volvo&cars[]=BMW

So you get your formated array in $_GET['cars']

print_r($_GET['cars']); // array('Saab', 'Volvo', 'BMW')

To get such a result in javascript you can use this kind of code

var cars = ['Saab', 'Volvo', 'BMW'],
    result = '';

for (var i = cars.length-1; i >= 0; i--) {
    results += 'cars[]='+arr[i]+'&';
}

results = results.substr(0, results.length-1); // cars[]=BMW&cars[]=Volvo&cars[]=Saab

Comments

0

Try to pass like this

?question_id=10&value=1&my_array=Saab,Volvo,BMW

and you can get like

<?php
    $my_array = explode(',',$_GET['my_array']);
?>

or you can try like this

$aValues = array('Saab','Volvo','BMW');
$url = 'http://example.com/index.php?';
$url .= 'aValues[]=' . implode('&amp;aValues[]=', array_map('urlencode', $aValues));

Comments

0

You may also try this:

?arr[]="Saab"&arr[]="BMW"

Using the jQuery getUrlParam extension I can get url variables very easily

or in PHP

var_dump($_GET);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.