How to pass multiple variable values ​​via url

I have an application that I am creating and I am stuck at a certain point.

I am trying to pass a variable with multiple values. Therefore, my URL will look like this:

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

The problem is that I'm not sure how I can do this. Variables should be used to retrieve data from the database. I am using PHP and not Javascript.

Any help would be great!

EDIT:
Here is the HTML that I have on 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> 

Thus, the user can select "Books and CD", and I will need to transfer these two values ​​in the variable "types".

+7
source share
6 answers

As pointed out in https://stackoverflow.com/a/3/3/100/... , you can use this method.

If you want PHP to process $ _GET ['select2'] as an array of parameters, just add square brackets to the name of the select element as follows: <select name="select2[]" multiple …

Then you can access the array in your PHP script

 <?php header("Content-Type: text/plain"); foreach ($_GET['select2'] as $selectedOption) echo $selectedOption."\n"; 
+5
source

Use &

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

That's what you need?

+7
source

There is an easy way to do this in PHP by calling http_build_query() and passing 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 url with $query .

+4
source

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

+2
source

I think you have the correct URL.

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

Then use PHP to get all the values ​​in the report.php page

 $variable = explode(",", $_GET["variable"]); // Output $variable[0] = "value1"; $variable[1] = "value2"; $variable[2] = "value3"; 
+2
source

You can use serialize , for example:

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

and get the data using unserialize , for example:

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

A serialization function that provides you with a persistent (string) version of an array type and can be restored using the unserialize function.

0
source

All Articles