Jump to content

Paging Issue with html form check boxes


joer80
 Share

Recommended Posts

I have a search page that has check boxes for selecting the styles of vehicle you want to see, but when you go to page 2, it does not save the style values.

The form and paging work great on anything that only contains one value, like a drop down, but just forgets anything like a checkbox that can have more than one thing when you leave the first page.

On post back when 2 things are checked, it makes a query string for each checkbox like this: (If I check the suv and pickup checkboxes)

?style%5B%5D=SUV&style%5B%5D=Pickup+Truck    (Basically is url encoded style[]=Value for each checkbox checked.)

I think the paging control does not seem to support having []'s in the query string name.  It always passes the style as empty, ie. style=

Does anyone know how to fix this and allow paging to work with check boxes?

Here is what I am working with.  It also shows you what the selector string is for testing. 

Thanks in advance for any advice you can offer!

Link to comment
Share on other sites

With or without encoding, the selector seems to work ok before paging.

"?style[]=SUV&style[]=Coupes"  does  "livefeed_Body=SUV|Coupes"

Here is the html:

<div class="checkbox">
<label>
<input type="checkbox" name="style[]" value="Convertible"> Convertibles
</label>
</div>

This is my php code:

if($_GET['style']){
	
  $styles = $_GET['style'];
  $styleList = '';
  
  foreach ($styles as $style){
	$styleList .= $sanitizer->selectorValue($style) . '|'; 	
  }
  
  //Add the Body Styles to the selector only if there is one.
  if(!empty($styles[0])){
	$selectorList['style'] = 'livefeed_Body=' . trim($styleList, '|'); //trim off the trailing pipe
  }
} //end if there is a style
Link to comment
Share on other sites

I am really not sure if this is the issue or not (in a hurry), but I have this same approach working, but I had to do this:

$pagination = $filter_results->renderPager(array('arrayToCSV' => false));

See Ryan's post about it here:

https://processwire.com/talk/topic/3472-enable-pagination-for-search-results/?p=38214

EDIT:

I think you are also missing whitelist. Read about it here:

https://processwire.com/talk/topic/1883-how-to-use-input-whitelist-with-checkboxes-or-any-array/

Edited by adrian
Link to comment
Share on other sites

This is what I use on a site. This was the second site I did with PW, so no guarantees that it is the most efficient way, but you can see how I am handling whitelisting with arrays.

<?php

if($input->get->filter_save || isset($input->get)) {
    // Search selector builder
    $search_string = '';
    $current_field = '';

    foreach($input->get as $field_name => $field_result){

        if (is_array($field_result)){
            foreach($field_result as $f_value){

                $input->whitelist($field_name, $field_result);

                if($current_field == $pages->get($f_value)->template){
                    $search_string .= '|' . (int)$f_value;
                }
                else{
                    $search_string .= ',' . $pages->get($f_value)->template . '=' . (int)$f_value;
                }
                $current_field = $pages->get($f_value)->template;

            }
        }
        else{
            if($field_name == 'search' && $field_result != ''){
                $search_string .= 'title|headline|summary|body|notes%=' . $sanitizer->selectorValue($field_result);
                $input->whitelist('search', $sanitizer->selectorValue($field_result));
            }
            elseif($fields->$field_name->type == 'FieldtypeDatetime' && $field_result != ''){
                $search_string .= ',' . $field_name . '=' . $sanitizer->selectorValue($field_result);
            }
        }
    }

    $search_string = trim($search_string,',');

}
else{
    $search_string = '';
}
Link to comment
Share on other sites

Ok, It looks like its empty because I am not white listing the get variables that are arrays.  I was just looping through all get vars and white listing them not checking to see if they are an array or not, but whitelist doesnt seem to work on arrays.

This is what I have come up with to loop through all GET Vars, sanitize and white list.  Does this look right?  If the get var is an array, I convert it is csv and white list that.

The only thing I am really iffy on, is if you sanitize the individual values, but whitelist the csv string.

//if there are get vars, loop through them to sanitize and whitelist(Use like this: $input->whitelist['sb'] for the sort by)
if(count($input->get)){
  foreach($input->get as $key => $value){
	  
	  //If its an array, I need to whitelist as csv
	  if(is_array($value)) {
		  
		$a = $value;
                $value = '';
		
                foreach($a as $k => $v){
			$v = $sanitizer->selectorValue($v);
			$value .= "$v,";
		}
		
                $value = rtrim($value, ",");
                $value = trim($value, '"');
		$input->whitelist($key, $value);
		
	  } else {
		$value = $sanitizer->selectorValue($value); 
                $value = trim($value, '"');
		$input->whitelist($key, $value);
	  }
  } //end loop through get
} //end if get vars
Link to comment
Share on other sites

That was the code for sanitizing and whitelisting all get vars, array or not, here is the code I ended up using to turn a set of checkboxes into a selector.  I bet it could be cleaned up and simplified though!

//Make Selector for Make Checkboxes
if($input->get->Makes) { 

  //Make sure get is an array
  if(!is_array($input->get->Makes)) { //if not array
	  $input->get->Makes = explode(',', $input->get->Makes);
  }
  
  $makesList = str_replace(',', '|', $input->whitelist['Makes']);
  
  if(!empty($makesList)){
	$selectorList['Makes'] = 'livefeed_Make=' . $makesList;
  }
}
// end Makes checkbox selector code
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...