Reputation: 39
I have page listing with schools, and search filter with CHECKBOX INPUTS.
For example one criteria is school's city. So when you submit the filter form URL looks like this: **
www.schools.com/schools/?city%5B%5D=1&city%5B%5D=2&city%5B%5D=3&city%5B%5D=4
And I would like to make it look like this.
www.schools.com/schools/**?city[]=1,2,3,4
So basicly I need solution for two problems:
1. How to pass multiple variables with same name via array with nice url?
2. How to keep special characters (like "[", "]") in the URL?
I use Codeigniter framework. Thank you guys.
// Edit For Anonymous
I would like to make this simple form:
<form action="" method="get">
<input name="kraj[]" type="checkbox" value="1">
<input name="kraj[]" type="checkbox" value="2">
<input type="submit" value="send">
</form>
When user checks both inputs, send data to url like that:
example.com/?kraj[]=1,2
not like this: example.com/?kraj[]=1&kraj[]=2
I know how to handle it after getting it in required format but I don't know how to send it like that.
Upvotes: 2
Views: 691
Reputation: 12017
You could make the URL look better by using -
to separate the values since that special character does not get changed. This works perfectly for me and should keep the check-boxes checked using the $_GET
method in addition to some JavaScript:
<?php
$city_array = explode('-',$_GET['city']);
?>
<input type="checkbox" value="1" <?php if (isset($_GET['city']) && in_array('1', $city_array)) { echo 'checked="checked"'; } ?>/>
<input type="checkbox" value="2" <?php if (isset($_GET['city']) && in_array('2', $city_array)) { echo 'checked="checked"'; } ?>/>
<input type="checkbox" value="3" <?php if (isset($_GET['city']) && in_array('3', $city_array)) { echo 'checked="checked"'; } ?>/>
<input type="checkbox" value="4" <?php if (isset($_GET['city']) && in_array('4', $city_array)) { echo 'checked="checked"'; } ?>/>
<form method='GET' action='' id='form'>
<input name='city' type='hidden' value='' id='hidden'/>
</form>
<input type='submit' onclick='checkboxarray()'/>
<script>
function checkboxarray() {
var checked = [];
for (var i = 0; i < document.getElementsByTagName('input').length; i++) {
var checkbox = document.getElementsByTagName('input')[i];
if (checkbox.checked) {
checked.push(checkbox.value);
}
}
var checked = checked.toString().replace(/,/g,'-');
document.getElementById('hidden').value = checked;
document.getElementById('form').submit();
}
</script>
For example, the new URL may be: www.schools.com/schools/?city=1-4
.
Upvotes: 2