2

My situation :

I am working on an shopping cart application and it contains some filters:

  1. Filter by color (checkboxes)

  2. Filter by style (checkboxes)

on selecting some of the colors my url becomes like this:

http://example.com/women/try.php?color=10,11,12,13

My issue :

  1. On unchecking some colors the related params are not getting cleared from the url.

  2. Also when I select some styles , I want the url to be like this:

    http://example.com/women/try.php?color=10,11,12,13&style=1,2,3

Please help me how to achieve this functionality.


My code :

<?php
    $colors = $_GET['color'];
    $sel_colors = explode(',', $colors); 
    foreach($sel_colors as $k=>$v) {
        $c['check'][$v] = $v;
    }

    for($i=10;$i<=14;$i++) { ?>
        <input type="checkbox" name="color[]" value="<?php echo $i; ?>"  <?php echo $check_value = ($c['check'][$i]) ? 'checked' : '0'; ?> >
        <label>Color #<?php echo $i.'--'.$check_value; ?></label><?php
    }
?><br/><br/><br/>
<input type="checkbox" name="type" value="1" >
<label>style #1</label>
<input type="checkbox" name="type" value="2" >
<label>style #2</label>
<input type="checkbox" name="type" value="3" >
<label>style #3</label>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js" ></script>
<script type="text/javascript">
    var baseUrl = 'http://website/women/try.php?color=';
    $(document).ready(function () {
        // listen to change event (customize selector to your needs)
        $('input[type=checkbox]').change(function (e) {
            e.preventDefault();
            if ($(this).is(':checked')) {
                // read in value
                var queryString = $(this).val();
                // loop through siblings (customize selector to your needs)
                var s = $(this).siblings();
                $.each(s, function () {
                    // see if checked
                    if ($(this).is(':checked')) {
                        // append value
                        queryString += ',' + $(this).val();
                    }
                });
                // jump to url
                window.location = baseUrl + queryString;
            }
        });
    });
</script>
Community
  • 1
  • 1
  • Possible duplicate of [Append the checkbox selected values with url as querystring](http://stackoverflow.com/questions/20371232/append-the-checkbox-selected-values-with-url-as-querystring) – Atif Tariq Feb 15 '16 at 18:23

2 Answers2

1

Use map() function to get all checked color and style checkbox values like following.

var baseUrl = 'http://website/women/try.php?color=';

$('input[name="color[]"], input[name="type"]').change(function () {
    var colors = $('input[name="color[]"]:checked').map(function () { return this.value; }).get().join();
    var styles = $('input[name="type"]:checked').map(function () { return this.value; }).get().join();

    window.location = baseUrl + colors + '&style=' + styles;
});
Ibrahim Khan
  • 20,616
  • 7
  • 42
  • 55
1

Here is a code snippet of working solution. Name of the color checkbox is changed from color[] to just color

var baseUrl = 'http://website/women/try.php?';
    $(document).ready(function () {

      // listen to change event (customize selector to your needs)
      $('input[type=checkbox]').change(function (e) {
        
        //Get all the selected color values
        var queryString = "color="+$('[name="color"]:checked')
                          .map(function() {return this.value;}).get().join(',');
        
        //Append all the selected styles
        queryString += "&style="+$('[name="type"]:checked').map(function() {return this.value;}).get().join(',');
      
        //reload page - commented for this snippet
        //window.location = baseUrl + queryString;
        alert(baseUrl + queryString);
      });
      
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<!-- Color filter -->
<input type="checkbox" name="color" value="1"  checked >
    <label>Color 1</label>
<input type="checkbox" name="color" value="2"  checked >
    <label>Color 2</label>
<input type="checkbox" name="color" value="3"  checked >
    <label>Color 3</label>
<input type="checkbox" name="color" value="4"  checked >
    <label>Color 4</label>

<BR><BR>
  
<!-- Style filter -->
<input type="checkbox" name="type" value="1" >
<label>style #1</label>

<input type="checkbox" name="type" value="2" checked>
<label>style #2</label>

<input type="checkbox" name="type" value="3" checked>
<label>style #3</label>
Nadeem Manzoor
  • 760
  • 5
  • 14