title: Alternative Way to Pass Checkbox Array to $_GET - Besides Square Braces
body: <p>I understand that if I use a checkbox value of name[], then I will receive a data array on the server (when using PHP), named 'name[]'. This has worked fine for me, but I'm running into some URL sizes that could cause issues with less robust IE browsers and all the encoded square braces are killing me in this area, easily causing the URL length to be at least 4-6 times longer than what it could possibly be, if another way were available. Is there a reliable method using javascript (jquery syntax even better) to intercept a checkbox forms values and convert them into something like this:</p>
  
  <p>"&amp;checkboxarray=1-23-45-13-67"</p>
  
  <p>I figure that on the other end I can easily explode $_GET['checkboxarray'] into an actual array and go from there as I usually do with matching the selection array against the options array, etc... I just don't know if it's possible or how to create alter the submit process.</p>
  
  <p>Side note, isn't passing "name[]" to a URL non-standards compliant anyways? Every browser I've used auto encodes it, but not Mozilla, however it seems to work fine.</p>
  
  <p>EDIT: I need to create paginated links, which is why I'm using GET, instead of POST. This is also and industrial search, very comprehensive, lots of power user options.</p>
  
  <p><strong>UPDATE &amp; Answer:</strong> I managed to come up with my own answer. For anyone else who wants to take advantage of $_GET's easy pagination workflow but you need to pass large data arrays and are worried about URL length, here's a simplistic way to compact it all down into one variable with dash separated values:</p>
  
  <p>NOTE: I <em>HIGHLY</em> suggest you first make sure any dynamic arrays generated from queries start with 1 rather than 0 if your going to recheck values after submit, here's how, since 0 can be a real pain in the neck to work with in PHP conditional statements:</p>
  
  <pre><code>$your_array= array();
  
  array_unshift($your_array,'');
  unset($your_array[0]);
  </code></pre>
  
  <p>In your HTML code, set all checkbox input names to "something[]" and underneath this set of inputs, create a hidden input with the name "something", I suggest you make them match, but I suppose you could use another name, just make sure the hidden one is missing the square braces, also set the hidden input value to "":</p>
  
  <pre><code>&lt;input type="text" name="something[]" value="1"&gt;
   .....
  &lt;input type="text" name="something[]" value="20"&gt;
  &lt;input type="hidden" name="something" value=""&gt;
  </code></pre>
  
  <p>Javascript: NOTE, requires jquery... this grabs all the "something[]" input values and forms a dashed array while killing off "something[]" values from being submitted and only submitting "something".</p>
  
  <pre><code>$('#submitbutton').click(function(){
  
        var searchIDs = $('input[name="something[]"]:checked').map(function(){
        return $(this).val();
        }).get(); 
  
       var IDstring = searchIDs.toString();
       var newvar = IDstring.replace(/,/g, '-');
       $('input[name="something"]').val(newvar);
       $('input[name="something[]"]:checkbox').prop("checked", false);
  
  
     });
  </code></pre>
  
  <p>On the server side, simply explode the 'something' value from $_GET.</p>
  
  <pre><code>$somethingArray = explode('-',$_GET['something']);
  </code></pre>
  
  <p>There it is! Hope it helps someone in the future make their GET sent arrays more compact. Bonus: Avoids sending unsafe characters in the URL, Mozilla doesn't appear to auto encode square braces, at least not my version of it on Linux Mint ;)</p>
  
  <p>Update:</p>
  
  <p>I just implemented this code on a big country checkbox form with 284 possible selections. With my old code, even using 'c[]' as the name, my character count was around 3100 characters, with the new approach, my character count now rings in at just 1109. Worth the effort.</p>