title: How to pass large array value from javascript to php
body: <p>My requirement is to make multiple xml request and to store the data in an array so that if i get say for example 4000 records I can display 40 records in a page and can do pagination. So if i goto page2 I can get the data from the array instead of making the xml request again.</p>
  
  <p>Currently I am using a php array variable to store large amount of data and on click a button javascript function will call the same php page again using ajax request through query string. The problem is that the query string can't hold large arrays.</p>
  
  <p>Here is my code -</p>
  
  <ol>
  <li><p>Passing php array to javascript variable</p>
  
  <pre><code>echo '&lt;script type="text/javascript"&gt;/* &lt;![CDATA[ */';
  echo 'var allProd = '.json_encode($allProd);
  echo '/* ]]&gt; */&lt;/script&gt;';
  </code></pre></li>
  <li><p>Using form calling javascript function through button click</p>
  
  <pre><code>&lt;form id="new_page" name="new_page" method="post" action="" &gt;
      &lt;td&gt;&amp;nbsp;&amp;nbsp;Jump to page&amp;nbsp;&lt;input name="neggPage" id="neggPage"  class="pageText" style="height:20px; width:40px"/&gt;&lt;/td&gt;
      &lt;input type="hidden" name="hideProd" value="&lt;?php echo htmlentities(serialize($allProd)); ?&gt;" /&gt;
      &lt;td&gt;&lt;input type="Submit" name="neggBut" value="Go" class="pageButton" onClick="get_results(document.getElementById('neggPage').value, &lt;?php echo $sInd; ?&gt;, &lt;?php echo $rndTot; ?&gt;, &lt;?php echo $totEnt; ?&gt;, allProd);"&gt;
      Total pages &lt;?php echo $rndTot; ?&gt;&lt;/td&gt;
      &lt;td height="40"&gt;&amp;nbsp;&lt;/td&gt;
     &lt;/tr&gt;&lt;/table&gt;
   &lt;/form&gt;
  </code></pre></li>
  <li><p>Javascript function</p>
  
  <pre><code>&lt;script&gt;
  function get_results(PageNo, SelIndex, totPage, totEnt, allProd)
  {
  var allProd1 = '';
  for (i=0; i &lt; allProd.length; i++)
  {
      allProd1 += '&amp;q[' + i + ']=' + encodeURIComponent(JSON.stringify(allProd[i]));
  }
  
  if (PageNo &gt; totPage || isNaN(PageNo))
  {
      alert ('Please enter the available pages');
  }
  else
  {
  var neggProd = sessionStorage.getItem('Product1');
  var cntryCode = sessionStorage.getItem('Cntry'); 
  var sortType;
  var ordType;
  if (SelIndex == 0) {sortType = 'bestmatch';}
  else if (SelIndex == 1) {sortType = 'bestseller';}
  else if (SelIndex == 2) {
      sortType = 'pricehigh';
      ordType = 'dsc';
  }
  else if (SelIndex == 3) {
      sortType = 'pricelow';
      ordType = 'asc';
  }
  
  document.getElementById("sl_api").innerHTML="";
  document.getElementById("sl_api1").setAttribute("style","background:     url('KartEazy/loading.gif') #edeeee no-repeat center center; background-size:20px 20px;");
  
  if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
  else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200)
      {
      document.getElementById("sl_api").innerHTML= xmlhttp.responseText;
      }
    }
  
  xmlhttp.open("GET",'KartEazy/Skimlinks/Skimlinks.php?PageNum=' + PageNo + '&amp;neggProd=' + neggProd + '&amp;cntryCode=' + cntryCode + '&amp;sortType=' + sortType + '&amp;ordType=' + ordType + '&amp;totEnt=' + totEnt + allProd1, true);
  xmlhttp.send();
  setInterval(function()    {
  document.getElementById("sl_api1").setAttribute("style","background: #edeeee;")},4500);
  }
  }
  &lt;/script&gt;
  </code></pre></li>
  <li><p>In the same page now I am trying to get the array</p>
  
  <pre><code>if(isset($_REQUEST['q']))
  {
  $totEnt = $_REQUEST['q'];
  }
  </code></pre></li>
  </ol>
  
  <p>I tried using hidden form variable but even $_POST also not accepting large arrays. Could any of you please give me a solution or suggestion. It will be very helpful for me.</p>
  
  <p>Even storing the data in database or session variable will not solve my requirement. Because if I use database I can't retrieve data for multiple queries and even php session variable will be overwritten if the user uses multiple tabs for the same url.</p>