Brucep
Brucep

Reputation: 41

passing two values for same checkbox field in get form

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?

Upvotes: 2

Views: 3939

Answers (3)

Patrick
Patrick

Reputation: 716

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"]).

Upvotes: 2

Simon Kohlmeyer
Simon Kohlmeyer

Reputation: 156

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

Upvotes: 0

Gary Richter
Gary Richter

Reputation: 537

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

Upvotes: 0

Related Questions