How to pass multiple values of a variable through a URL
Asked Answered
L

6

8

I've an application that I'm building, and I'm stuck at a certain point.

I'm trying to pass a variable that has multiple values. So my URL will look like:

localhost/report.php?variable=value1, value2, value3

Problem is I'm not sure how I can do this. The variables are to be used to retrieve data from a database. I'm using PHP and no Javascript.

Any help would be great!

EDIT:
Here is the HTML I have in my page where the variables are selected:

<select name="types" size="19" multiple>
    <option value="all" selected>All Types</option>
    <option value="book" selected>Books</option>
    <option value="cd" selected>CD</option>
</select>

So a user could select Books and CD, and I would need to pass these two values in the "types" variable.

Landlord answered 12/8, 2012 at 19:31 Comment(6)
how about a serialised array?Andra
Are you using a from to send this? if so php should just get it as an array in $_POST['types']?Hypothermal
@wgcrouch I am using a form to send it. But when I do send it the URL ends up like report.php?variable=value1&variable=value2Landlord
Possible duplicate #2407784Siamang
you need to change the name of your select element to types[]Hypothermal
@DouglasA.Crosby I used the method in that link you posted and it worked. If you put it as an answer I'll mark it as correct.Landlord
S
5

As noted at https://mcmap.net/q/108307/-how-to-get-multiple-selected-values-of-select-box-in-php, you can use this method.

If you want PHP to treat $_GET['select2'] as an array of options just add square brackets to the name of the select element like this: <select name="select2[]" multiple …

Then you can acces the array in your PHP script

<?php
header("Content-Type: text/plain");

foreach ($_GET['select2'] as $selectedOption)
  echo $selectedOption."\n";
Siamang answered 13/8, 2012 at 17:7 Comment(0)
S
11

Use &

localhost/report.php?variable=value1&val2=value2&val3=value3

Is that what you are after?

Soredium answered 12/8, 2012 at 19:32 Comment(3)
that's multiple variables, not one variable with multiple valuesAndra
Sort of, what I really need is to add all of the values to that variable. So in the PHP code I can just use $variable = $_GET['variable'], so then I can use $variable in the query.Landlord
If both pages are on the same domain, you can use the Session variables? Session["myStringName"] = someObject then simply read it back by casting to what you need. Sorry, but my PHP isn't so good, though.Soredium
A
6

There is a simple way to do this in PHP by calling http_build_query() and pass your values as an indexed array. You would do something like:

$value_array = array('types' => array('book', 'cd'));
$query = http_build_query($value_array);

Then generate the url using $query.

Aday answered 12/8, 2012 at 19:35 Comment(0)
S
5

As noted at https://mcmap.net/q/108307/-how-to-get-multiple-selected-values-of-select-box-in-php, you can use this method.

If you want PHP to treat $_GET['select2'] as an array of options just add square brackets to the name of the select element like this: <select name="select2[]" multiple …

Then you can acces the array in your PHP script

<?php
header("Content-Type: text/plain");

foreach ($_GET['select2'] as $selectedOption)
  echo $selectedOption."\n";
Siamang answered 13/8, 2012 at 17:7 Comment(0)
S
3

I think you have your URL correct

localhost/report.php?variable=value1,value2,value3

Then use PHP to get all of the values on the report.php page

$variable = explode(",", $_GET["variable"]);

// Output
$variable[0] = "value1";
$variable[1] = "value2";
$variable[2] = "value3";
Siamang answered 12/8, 2012 at 19:41 Comment(0)
H
2

try localhost/report.php?variable[]=value1&variable[]=value2 will give you an array in php

Hypothermal answered 12/8, 2012 at 19:35 Comment(0)
M
0

You can use serialize like:

echo '<a href="index.php?var='.serialize($array).'"> Link </a>';

and get data using unserialize like:

$array= unserialize($_GET['var']);

serialize function giving you storable (string) version of array type, and can be restored by unserialize function.

Moon answered 12/8, 2012 at 19:46 Comment(1)
Interesting idea. If I were to bookmark or copy a index.php?var='.serialize($array).' link, would I be able to reload that later or is the serialized data lost in the session?Soredium

© 2022 - 2024 — McMap. All rights reserved.