Re: Multiple HTTP variables of the same name

From: Date: Thu, 02 Nov 2000 00:00:53 +0000
Subject: Re: Multiple HTTP variables of the same name
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-4110@lists.php.net to get a copy of this message
> I was looking at how to read HTML form variables and I found the > "$HTTP_GET_VARS", which seemed to be the right one, but it doesn't seem > to allow multiples of the same named query parameter. > > For example, an HTTP GET method like this: > > > http://...../php.html?site=1&site=2&site=3&question=6&question=7 > > > gives site=3 and question=7 in the PHP script that is called. ie, > php.html. > > Is there some way of getting multiple query parameters of the same > name? > Your best bet would probably be to change your form method from "GET" to "POST". You can then access the variables via $HTTP_POST_VARS instead of $HTTP_GET_VARS. You still cannot have more than one variable by the same name since they will just keep overwriting the values, but you can pass in an array of values. I'm going to assume based on your example that a user is allowed to type in three different site names so you would want something like this. <FORM METHOD="POST" ACTION="your_action"> <INPUT TYPE="TEXT" NAME="site[0]" VALUE=""> <INPUT TYPE="TEXT" NAME="question[0]" VALUE=""> <INPUT TYPE="TEXT" NAME="site[1]" VALUE=""> <INPUT TYPE="TEXT" NAME="question[1]" VALUE=""> <INPUT TYPE="TEXT" NAME="site[1]" VALUE=""> <INPUT TYPE="TEXT" NAME="question[1]" VALUE=""> </FORM> When you post that back to your php script you can use: <? print_r($HTTP_POST_VARS); ?> to see the values. You will have an array at $HTTP_POST_VARS['site'] and $HTTP_POST_VARS['question'] which you can loop through and do what you please with. You can also loop through values and output the form dynamically filling in the values in the brackets as you go. Also, you could so something like this: <FORM METHOD="POST" ACTION="your_action"> <INPUT TYPE="TEXT" NAME="data[][site]" VALUE=""> <INPUT TYPE="TEXT" NAME="data[][question]" VALUE=""> <INPUT TYPE="TEXT" NAME="data[][site]" VALUE=""> <INPUT TYPE="TEXT" NAME="data[][question]" VALUE=""> <INPUT TYPE="TEXT" NAME="data[][site]" VALUE=""> <INPUT TYPE="TEXT" NAME="data[][question]" VALUE=""> </FORM> You will then have an array accessed via $HTTP_POST_VARS['data'][x]. As you can tell this will allow you to easily add as many elements as you would like and simple loop through them. Hope that helps you out. - Jamie

« previous php.db (#4110) next »