0

I have a form something like this:

<form action="video-sort.php" method="get">      
<h3>Game</h3>
<ul> 
    <li><input id="game-180man" type="checkbox" name="game" value="180man">180man</li>
    <li><input id="game-18man"  type="checkbox" name="game" value="18man">18man</li>
    <li><input id="game-mtt"    type="checkbox" name="game" value="MTT">MTT</li>        
</ul>       
<h3>Type</h3>
<ul>
    <li><input id="type-lecture"  type="checkbox" name="type" value="Lecture">Lecture</li>
    <li><input id="type-liveplay" type="checkbox" name="type" value="Liveplay">Liveplay</li>
    <li><input id="type-tutorial" type="checkbox" name="type" value="Tutorial">Tutorial</li>
</ul>       
<input type="submit" />     
</form>

If the first of each set is checked, I get the URL:

video-sort.php?game=180man&type=Lecture

Great! But when more than one checkbox in each category is checked, I get (if I check all, for example):

video-sort.php?game=180man&game=18man&game=MTT&type=Lecture&type=Liveplay&type=Tutorial

No good! It only takes the final argument.

Also, I tried a form where name=game[] and name=type[] but the URL ended up just adding the brackets to the names (ex: video-sort.php?game[]=180man&game[]=18man instead of putting them together.

I'd like to get the URL to read:

video-sort.php?game=180man,18man,MTT&type=Lecture,Liveplay,Tutorial

How could I accomplish this? Any help would be greatly appreciated!

EDIT: This is for a Wordpress site. I'm creating a post filter and for Worpdress to understand the query, to the best of my knowledge, the arguments need to be in a commma-separated list. If I use the brackets, I get the error:

Warning: preg_split() expects parameter 2 to be string, array given in /wp-includes/query.php on line 1694

(source of that file: http://phpxref.com/xref/wordpress/wp-includes/query.php.source.html)

3
  • 1
    hello @christina..why dont you use post method..?? Commented Jun 22, 2011 at 21:41
  • Two questions: 1) Do you really need/want to use GET instead of POST? 2) Why do you want the URL to read like that instead of changing the names to different and unique strings? Typically you would keep the name identical if you wanted radio buttons with a single select, not checkboxes with multiple options. Commented Jun 22, 2011 at 21:42
  • I'm sort of hacking at Wordpress to create a post filter function and this has been the most efficient way I've found to accomplish my purpose. :/ I'm not sure how I could get POST to accomplish the same thing... Commented Jun 22, 2011 at 21:43

2 Answers 2

2

That's correct,

?game[]=180man&game[]=18man

just adds keys to an array. Keep it like that.

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

6 Comments

Don't need to add keys. The keys are implicit 0, 1, 2, 3... Also you should use method="post" instead of get.
He meant "values" which was mostly clear. And there are reasons to use GET over POST; it's not a simple substitution.
@FinalForm: Why post? If the script only retrieves data and doesn't make any changes, then get is entirely appropriate. Especially for what appears to be some sort of search engine. POST would only be required if the GET url would be too long, or the requested operation isn't idempotent.
If @christina wants to get single value from array then @genesis's method is correct as @christina can get single value and no need of loop.
@GitsD: You said somethink that isn't true. URL I wrote is accessable by <?php print_r($_GET['game']); ?> and it will return array ([0] => 180man [1] => 18man)
|
2

The proper way really is to use the array syntax (game[]), that way in your video-sort.php code you can fetch the whole array like this:

$games = $_GET['game'];
foreach ($games as $game) {
  // do something
}

If you really had to have the game parameter be a comma delimited list, you would probably want to use javascript (i.e. jquery) to fetch a list of checked values, create the list, and then append that to your form post. Too much work and too prone to error. What if one of your games had a comma in its value?

Comments

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.