3

I'm using checkboxes to search a mysql database I have created. As the checkboxes all use the same name, when I use $_GET, it only gets the last value in the URL.

For example: http://www.website.com/search.php?features=Textures&features=Items&style=Realistic&submit=search

would only return Items, and would override Textures.

Is there a way to store both the values, and then to use these values to search my database?

2
  • If you insist on the key always being feature then you will only get the last one set in $_POST, and you'd have to process the raw input yourself. If you don't mind the parameter name being feature[] then $_POST will contain an array called feature containing all the values Commented Jun 21, 2018 at 8:56
  • Processing the raw POST data yourself would involve reading from php://input and building the request data yourself. php.net/manual/en/wrappers.php.php#wrappers.php.input Commented Jun 21, 2018 at 8:57

4 Answers 4

8

PHP is a little odd here. Using its standard form data parser, you must end the name of the controls with [] in order to access more than one of them.

<input type="checkbox" name="foo[]" value="bar">
<input type="checkbox" name="foo[]" value="bar">
<input type="checkbox" name="foo[]" value="bar">

Will be available as an array in:

$_GET['foo'][]

If you don't want to rename the fields, then you will need to get access to the raw data ($_SERVER['REQUEST_URI']) and parse it yourself (not something I'd recommend).

Sign up to request clarification or add additional context in comments.

3 Comments

@MenelaosKotsollaris — Not in any system I've seen.
@Quentin PHPStorm ?
… is an IDE, and if it complains about that HTML then it is horrifically broken (or misconfigured).
2

I have never tried it with $_GET, but if you use $_POST you can do something like this:

<input type="checkbox" name="car[]" value="honda" /> Honda
<input type="checkbox" name="car[]" value="ford" /> Ford
<input type="checkbox" name="car[]" value="toyota" /> Toyota
// note the [] in the name

so that $car = $_POST['car'] is an array

You can try it with $_GET as well and see.

Comments

1

Name your checkbox elements "features[]" in html. That way they will be passed as an array.

Comments

-1

you need to make the NAME for that variable a HTML array'd variable so on the form, it would be

<input name='features[]'/>

2 Comments

an array is an array is an array. in a html form element, it can be an array element when using [] which is how it's passed as an array. sure you can nitpick that array'd is not the same as saying "an HTML element defined as to be a part of an array of that name with the option of specifying indexes within the square brackets"
I won't go so far as to downvote this, but while PHP will make an array from the data, as far as the URL and HTML is concerned, it is just a bunch of things with the same name. Most languages used on the server will let you get an array of data from any group of form controls with the same name, with no need for the special naming convention that PHP demands.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.