2

I have a form with a method="get" and right now it looks like this:

    <form>
            <input type="submit" id="searchsubmit" value="Sort Projects" class="project-sort" />
            <input type="checkbox" name="type" class="type-checkbox" value="type1"> 
            <label for="type1">Type 1</label>
            <input type="checkbox" name="type" class="type-checkbox" value="type2"> 
            <label for="type1">Type 2</label>
            <input type="checkbox" name="type" class="type-checkbox" value="type3"> 
            <label for="type1">Type 3</label>
    </form>

Yet, when I select two of them, the url it sends me to looks like this
/?type=type1&type=type2

I've already tried adding [ ] to the input name, and when that happens, the url appears like:

/?type%5B%5D=type1&type%5B%5D=type2

any idea what I'm doing wrong here?

Brucep
  • 41
  • 1
  • 6
  • what are you expecting to happen? – Daniel A. White Sep 11 '13 at 23:53
  • Might be some helpful information here: http://stackoverflow.com/questions/12628717/encoded-url-with-square-brackets-different-behaviour-in-chrome-firefox-ie – showdev Sep 11 '13 at 23:55
  • I would like for the url to appear like /?type=type1,type2 – Brucep Sep 11 '13 at 23:59
  • `$_GET` does not format variables that way. That would only happen if the value you are submitting is `type1,type2`. – showdev Sep 12 '13 at 00:01
  • Gotchya, is there anyway to retrieve that information as an array from the given url structure of type=type1&type=type2? – Brucep Sep 12 '13 at 00:10
  • This might be helpful: http://stackoverflow.com/questions/353379/how-to-get-multiple-parameters-with-same-name-from-a-url-in-php#answer-353437 – showdev Sep 12 '13 at 00:16

3 Answers3

2

Use different names for each checkbox, (don't use name="type" for each one)

<form>
  <input type="submit" id="searchsubmit" value="Sort Projects" class="project-sort" />
  <input type="checkbox" name="type1" class="type-checkbox" value="type1"> 
  <label for="type1">Type 1</label>
  <input type="checkbox" name="type2" class="type-checkbox" value="type2"> 
  <label for="type2">Type 2</label>
  <input type="checkbox" name="type3" class="type-checkbox" value="type3"> 
  <label for="type3">Type 3</label>
</form>

Matching names are for groups of radio buttons, not checkboxes.
Generally speaking, matching names are for groups of radio buttons, not checkboxes. There are some reasons to use matching names (mentioned below), but, obviously there are issues involved when doing so.

Perhaps OP could manually explode or preg_split the entire query string on the receiving end, then create an array of all the "type" values, (i.e. don't use $_GET["type"]).

Community
  • 1
  • 1
Patrick
  • 716
  • 5
  • 18
  • But I need them to be in lumped together, as the name corresponds to a taxonomy that is used for searching. – Brucep Sep 11 '13 at 23:56
  • One can group checkboxes by using arrays `[]`, just like any other form element. It just doesn't have the same functionality as radio buttons. – showdev Sep 11 '13 at 23:57
  • When I add [] to the name, it outputs the url above ('/?type%5B%5D=type1&type%5B%5D=type2') – Brucep Sep 12 '13 at 00:00
  • @Brucep Yes, I saw that in your question. The brackets are being encoded. What I'm saying is that this statement is not accurate: "Matching names are for groups of radio buttons, not checkboxes." – showdev Sep 12 '13 at 00:06
  • @showdev, I see... so if one uses the same name for checkboxes, it would be so that they can later use var boxes = document.getElementsByName("Type"); Correct? But, in that case, they will always have this form submission problem that Brucep is describing, right? – Patrick Sep 12 '13 at 00:26
  • @Patrick I suppose that would be one reason. I was thinking you might use brackets to format the data as an array, rather than having to build an array later in PHP using multiple independent variables. In my tests, I cannot reproduce the bracket encoding problem. When I submit the form, the URL contains brackets. If I manually paste a URL with encoded brackets, my browser automatically decodes the URL back to brackets. A chat discussion might be appropriate here! – showdev Sep 12 '13 at 00:32
0

Using the GET action on a form will put it in the querystring multiple times. But when the receiving page obtains the values from the querystring, it will put them together.

Querystring looks like: /?type=type1&type=type2

When you obtain the value of parameter "type" from the query string, the value will be "type1, type2"

Using classic ASP, when obtaining information from the forms collection:

query string: http://domain.com/page.asp?type=type1&type=type2

gathering information like this:

<%
    response.write("Value of type is: " & request("type"))
%>

outputs to the page:

Value of type is: type1, type2

Gary Richter
  • 526
  • 4
  • 16
  • I have never seen this behavior. If you set two identical keys in an array, one overwrites the other -- they are not concatenated. – showdev Sep 12 '13 at 00:02
  • I verified on my web server before posting. Created a quick ASP page, posted his code using GET. The query string reads as he mentioned. I obtained my value from the form collection (using myVar = request("type") and output the value to the screen. The value appeared as "type1, type3" (when I checked 1 and 3 obviously). If you submitted the form using POST, you wouldn't see it in the querystring but the resulting value when pulled from the form collection would correctly match obtaining from the query string. – Gary Richter Sep 12 '13 at 00:06
  • I'm not sure what the downgrade is for. I've fully tested and my response is correct. – Gary Richter Sep 12 '13 at 00:12
  • Does not work this way in PHP. Can you verify this or refer to documentation? – showdev Sep 12 '13 at 00:13
  • I tried this with the request, and it's throwing an error: Fatal error: Call to undefined function request() – Brucep Sep 12 '13 at 00:14
  • The post does not specify PHP. I can send you a link to the test page I created if you want to see it. – Gary Richter Sep 12 '13 at 00:14
  • Sorry, yes I'm dealing with PHP – Brucep Sep 12 '13 at 00:15
  • It seems that ASP *does* concatenate duplicate `$_GET` variables: http://stackoverflow.com/questions/17975148/passing-multiple-url-parameters-to-classic-asp#answer-17977051 I removed my downvote. (Does that mean you can set two keys the same in an array, too?) – showdev Sep 12 '13 at 00:18
0

If you use type[] as the names, the url will look like /?type%5B%5D=type1&type%5B%5D=type2 as you have already discovered.

This is the urlencoded[1] form of /?type[]=type1&type[]=type2. ([ encodes to %5B and ] encodes to %5b) Using this syntax, php will make type an array containing the values "type1" and "type2"

An example:

<html>
  <head><title>test</title></head>
  <body>
    <pre>$_GET is <?php var_dump($_GET); ?></pre>
    <hr/>
    <form method="get" action="">
      <input type="submit" id="searchsubmit" value="Sort Projects" class="project-sort" />
      <input type="checkbox" name="type[]" class="type-checkbox" value="type1"> 
      <label for="type1">Type 1</label>
      <input type="checkbox" name="type[]" class="type-checkbox" value="type2"> 
      <label for="type1">Type 2</label>
      <input type="checkbox" name="type[]" class="type-checkbox" value="type3"> 
      <label for="type1">Type 3</label>
    </form>
  </body>
</html>

Try this and you will see the structure of the values you get. If you, for example, check the first and third box, the output above the form will look like this:

$_GET is array(1) {
  ["type"]=>
  array(2) {
    [0]=>
    string(5) "type1"
    [1]=>
    string(5) "type3"
  }
}

[1] http://en.wikipedia.org/wiki/Percent-encoding

Simon Kohlmeyer
  • 157
  • 1
  • 8