2021年3月5日 星期五

php 同時傳送超過1000個變數時的做法


php 同時傳送超過1000個變數時,比方
  1. <form id="myForm">
  2. <?php
  3. for ($i=0; $i <1200 ; $i++) {
  4. echo '<button type="submit">送出</button>';
  5. }
  6. ?>
  7. <form>
此時會報錯誤
  1. PHP Warning: Unknown: Input variables exceeded 1000. To increase the limit change max_input_vars in php.ini.
雖然可以照它說的去更改 max_input_var 的數字,但終歸無法一牢永逸(因為我的變數一直在增加)
所以上網找到這個解法:將1000個變數的值集中到一個變數中,接收後再拆開來使用


在 html 的部份,加上一變數,在送出前集中所有變數的值到此一變數
  1. <input name="storeids" type="hidden" />
  2. <button type="submit" onclick="changeidstostring();">送出</button>
  1.  
在 js 的部份這樣寫
  1. function changeidstostring() {
  2. $('#myForm').submit(function() {
  3. var storeids = $("input[name='ids[]']").map(function(){return $(this).val();}).get();
  4. $("input[name='storeids']").val(storeids);
  5. $("input[name='ids[]']").attr("disabled","disabled"); // 將原來會產生很多變數的元素disabled 掉,就不會傳送了
  6. });
  7. }
在接收的部分這樣寫
  1. $ids = explode(",", $_POST['storeids']); // 因為參數太多(超過1000個,所以改成先集中到某個參數,傳送過來再拆解)

沒有留言:

張貼留言