$_GET and Multiple Selections

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jcobban
Forum Commoner
Posts: 41
Joined: Mon Mar 08, 2010 7:40 am

$_GET and Multiple Selections

Post by jcobban »

I have created a PHP page that returns XML. The following is expected to return the combined results for districts 89 and 90, but the PHP script, using $_GET only sees the value 90. I cannot see anything in the documentation that explains this. If I have to I will rewrite the code to use POST, but that is a significant change that will require global changes to my web-site.

http://www.jamescobban.net/database/Cen ... istrict=90
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: $_GET and Multiple Selections

Post by AbraCadaver »

You have two variables with the same name. How would you propose to access both of them with the same name but different values?

You can either do something like: District=89,90 or use an array: District%5B%5D[]=89&District[]=90 which would be encoded to District%5B%5D=89&District%5B%5D=90
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
jcobban
Forum Commoner
Posts: 41
Joined: Mon Mar 08, 2010 7:40 am

Re: $_GET and Multiple Selections

Post by jcobban »

That is how a multiple select is represented by the browser. I do not have any control over that.

According to a number of sample coding web pages if I had used POST the value of $_POST["District"] is an array containing the multiple values. However using GET the value of $_GET["District"] is just the last value. I cannot find any documentation to explain that difference. Before I rewrite a dozen web pages and a Javascript library to use POST instead of GET, I would like to see a documented explanation for why I have to do all that work. Obviously it is also a lot easier to test a PHP script that uses GET than one that uses POST.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: $_GET and Multiple Selections

Post by AbraCadaver »

I see. You didn't mention in your first post that you were using a form. I thought you were constructing the URL. It works the same for get and post, you have to represent the select as an array:

Code: Select all

<select name="District[]" multiple>
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
jcobban
Forum Commoner
Posts: 41
Joined: Mon Mar 08, 2010 7:40 am

Re: $_GET and Multiple Selections

Post by jcobban »

AbraCadaver wrote:I see. You didn't mention in your first post that you were using a form. I thought you were constructing the URL. It works the same for get and post, you have to represent the select as an array:

Code: Select all

<select name="District[]" multiple>
Interesting. Where is this documented? According to the HTML 4.1 specification:

"NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".")."

There is nothing to indicate that square brackets are permitted. After a quick search I cannot find an example of the syntax that you suggest. In any event the URL that I used in my example is what is sent by the browser for a multiple select with GET.

However you were right, my application does not actually use a form to construct the URL. It is created in Javascript. So I am not limited to what a form would generate, except that I know from various examples, at least with POST, that the multiple selections appear in PHP as an array value.

I have continued experimenting. I did get the application working by passing the value of District as a comma separated list, although strangely it was not my intent to do so. I actually constructed the parameter value as an array in the Javascript, but that got flattened to a comma separated string when the parameter was sent to the server. I still have not figured out how to pass an array.

I have changed my PHP script so that it now appears to work for a single value using GET, and for single or multiple values using POST. However if I pass multiple comma-separated values using GET it doesn't work.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: $_GET and Multiple Selections

Post by AbraCadaver »

It may be confusing, but NAME in this case is not a NAME token. It is a name attribute of the type CDATA token, which allows []. The [] syntax is not part of the HTML spec, it is the way that PHP has implemented to represent arrays in HTML forms:

http://www.php.net/manual/en/faq.html.p ... tml.arrays

If you look at my first post, that's probably the way you want to pass the array, or you can use the toString() method on the array and then explode() it in PHP. This happens automatically when you try and concatenate your array with a string in javascript as you have witnessed.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
jcobban
Forum Commoner
Posts: 41
Joined: Mon Mar 08, 2010 7:40 am

Re: $_GET and Multiple Selections

Post by jcobban »

AbraCadaver wrote:It may be confusing, but NAME in this case is not a NAME token. It is a name attribute of the type CDATA token, which allows []. The [] syntax is not part of the HTML spec, it is the way that PHP has implemented to represent arrays in HTML forms:
In my opinion the HTML specification is quite clear that name and id parameters should obey the restricted subset of CDATA. This is because the name and id values are used in the DOM, no matter what language it is implemented in. The PHP designers are therefore way out of line in unilaterally modifying the HTML specification. I presume that PHP has a good reason for ignoring parameters that are passed to it simply because they do not have [] appended to their name, and for causing problems for any implementation of the DOM. However such a justification totally escapes me. If I pass a parameter to a PHP script then that parameter should be passed to the script, period, not dumped in a bit bucket without warning!

Furthermore the documentation of something this fundamental should not be buried in a how-to section of the PHP documentation. It should be documented in a separate top level chapter, for example "Amendments to International Standards Required by PHP".
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: $_GET and Multiple Selections

Post by AbraCadaver »

jcobban wrote:
AbraCadaver wrote:It may be confusing, but NAME in this case is not a NAME token. It is a name attribute of the type CDATA token, which allows []. The [] syntax is not part of the HTML spec, it is the way that PHP has implemented to represent arrays in HTML forms:
In my opinion the HTML specification is quite clear that name and id parameters should obey the restricted subset of CDATA. This is because the name and id values are used in the DOM, no matter what language it is implemented in. The PHP designers are therefore way out of line in unilaterally modifying the HTML specification. I presume that PHP has a good reason for ignoring parameters that are passed to it simply because they do not have [] appended to their name, and for causing problems for any implementation of the DOM. However such a justification totally escapes me. If I pass a parameter to a PHP script then that parameter should be passed to the script, period, not dumped in a bit bucket without warning!

Furthermore the documentation of something this fundamental should not be buried in a how-to section of the PHP documentation. It should be documented in a separate top level chapter, for example "Amendments to International Standards Required by PHP".
Well, do what you will then. You've gotten sound advice and the reason as to why it is valid (even if you have misinterpreted it). There is no problem with the DOM here.

In closing, the parameters are passed to PHP and it uses them just as it should, the same as this (try it):

Code: Select all

$_GET['District'] = '89';
$_GET['District'] = '90';
echo $_GET['District'];
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply