How to handle max_input_vars restriction.

From time to time we could breach  max_input_vars restriction. Which basically limittation for server which allows only limited number of input server to reach out server. Usually this number is around 1000. In this short tutorial I will describe, how to easily avoid this problem.

Recently I had a project, when i was sending super long list of small numbers, and by default list was coming from the form – it was named order[] and had numeric values. Obviously server was getting error. Because server admin wasn’t present at the moment i figure out to find some kind of workaround. The easiest way to pass this restriction is just limit array size, to something reasonable.

Here was my buggy code:

var order = $(this).sortable("serialize");
   $.post("admin/sort", {order: order}, function(theResponse){
      ...
   });
}

Aa you can tell, I was passing data coming from jQuery UI Sortable Widget . Issue there was that data was already serialised. I wanted it as an array… I found this nice function to deserialise my array, and then I needed to join it to regular string, so I could send it via POST:

var order = $(this).sortable("serialize");
var s = $.unserialize(order); 
order = s["r[]"].join();

On the server side task wasn’t to complicated – instead of processing array coming from $my_arr = $_POST['order'] I had to explode it first $my_arr = explode(',', $_POST['order']);

And that solved my issue with  max_input_vars  restriction.