2
votes

I have Wordpress installed and I have this form on each category page sidebar:

<form name="tags" onChange="document.forms.tags.submit();"> <input type="checkbox" name="tag" value="tag1" <?php if((isset($_GET["tag"])) && $_GET["tag"] == "tag1") { echo "checked";}?>>tag1<br> <input type="checkbox" name="tag" value="tag2" <?php if((isset($_GET["tag"])) && $_GET["tag"] == "tag2") { echo "checked";}?>>tag2 </form>

The goal is to create an url like /?tag=tag1+tag2 and so on. With the above code I get the url like this: /?tag=tag1&tag=tag2.

I've searched for two weeks and tried a lot, but nothing works for me. I've tried for example

<input type="checkbox" name="tag[]" value="tag1" <?php if((isset($_GET["tag"])) && $_GET["tag"] == "tag1") { echo "checked";}?>>tag1

but then i get ?tag%5B%5D=tag1 and Wordpress don't find any results.

The form submit each time a checkbox is checked.

If I use radio input fields, then it works great, because then there's one value each time, but I want to pass multiple values with the same name and render a url like /?tag=tag1+tag2+tag3+tag4 etc.

Can anybody help me with this problem, because I don't know how to get this work for me. Thanks!

1

1 Answers

0
votes

this is what you want:

// This would output '/client/?s=word&foo=bar'
echo add_query_arg( 'foo', 'bar' );

take a look on add_query_arg() and get_query_var()

Consider on this example http://codepad.org/s08Jmseg

$url_string = $_SERVER['QUERY_STRING'];
// reuturn: tag=tag1+tag2+tag3

parse_str($url_string);

// $tag holds the value tag1+tag2+tag3
$string = urldecode( $tag );
$arr = explode(" ", $string);
$my_tag = "tag2";

if( in_array( $my_tag, $arr ) ) {
    // $key
    echo $my_tag . ' found!';
}
else {
    echo "Tag Not Found!";
}

//print_r($arr);