I'm trying to read in PHP a query string generated by an HTML form using <select multiple>
or several <input type="checkbox">
with the same name, and I want to see all values associated with each name. An example of such a form:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Form</title>
</head>
<body>
<h1>Form</h1>
<h2>Select</h2>
<form method="get" action="script.php">
<select multiple name="cantchange" size=5>
<option value="a">Alpha</option>
<option value="b">Bravo</option>
<option value="c">Charlie</option>
<option value="d">Delta</option>
<option value="e">Echo</option>
</select><br>
Ctrl+click to choose multiple items, then
<input type="submit" name="op" value="Submit">
</form>
<h2>Checkbox set</h2>
<form method="get" action="script.php">
<label><input type="checkbox" name="cantchange" value="f"> Foxtrot</label><br>
<label><input type="checkbox" name="cantchange" value="g"> Golf</label><br>
<label><input type="checkbox" name="cantchange" value="h"> Hotel</label><br>
<label><input type="checkbox" name="cantchange" value="i"> India</label><br>
<label><input type="checkbox" name="cantchange" value="j"> Juliett</label><br>
<input type="submit" name="op" value="Submit">
</form>
</body>
</html>
This produces a query string of the following form:
cantchange=a&cantchange=b&cantchange=c&op=Submit
But when PHP parses this query string into $_GET
, it sees only the last value with each name.
Answers to "How to get multiple selected values of select box in php?" and its duplicates recommend appending []
to the name
attribute of each select
or input
element in the form to make PHP collect values into an array. But this assumes that the author of the script that interprets the form can change the form. This isn't always possible. And even when I can change the form, adding the brackets reveals that the application is written in PHP, tempting intruders to try PHP-specific exploits against the server first.