目前,我正在使用动态数量的输入字段处理大型表单,它可能会变得非常大(超过3k或更多变量)并且会导致“max_input_vars”问题。选项。 我正在为此问题寻找解决方法(我对增加&max;最大输入值'选项的价值不感兴趣),所以目前我正在尝试加入所有表单字段值到单个字段(使用jQuery serialize()方法),然后在服务器端重新创建变量。 这就是我到目前为止所做的:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Workaround for max_input_vars</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<?php
if($_SERVER["REQUEST_METHOD"]==="POST" && !empty($_POST["data"])) {
$vars = explode("&", $_POST["data"]);
$data = array();
foreach($vars as $var) {
parse_str($var, $variable);
assign_var($_POST, $variable);
}
echo "<pre>";
var_dump($_POST);
echo "</pre>";
}
function assign_var(&$target, $var) {
$key = key($var);
if(is_array($var[$key]))
assign_var($target[$key], $var[$key]);
else {
if($key==0)
$target[] = $var[$key];
else
$target[$key] = $var[$key];
}
}
?>
<form id="myForm" method="POST">
<input type="text" name="var_1" value="<?php echo htmlentities("double quote: \"hello\"");?>"/><br/>
<input type="text" name="var_2" value="<?php echo htmlentities("single quote: 'hello'");?>"/><br/>
<input type="text" name="var_3" value="<?php echo htmlentities("text with & ampersant");?>"/><br/>
<input type="text" name="var_4" value="<?php echo htmlentities("animals");?>"/><br/>
<input type="text" name="matrix[]" value="1"/><br/>
<input type="text" name="matrix[]" value="2"/><br/>
<input type="text" name="matrix[]" value="3"/><br/>
<input type="text" name="matrix[]" value="4"/><br/>
<input type="text" name="matrix[]" value="5"/><br/>
<input type="text" name="matrix[]" value="6"/><br/>
<input type="text" name="matrix[]" value="7"/><br/>
<input type="text" name="var_5" value="abc"/><br/>
<input type="text" name="var_6" value="bcd"/><br/>
<input type="text" name="var_7" value="cde"/><br/>
<input type="text" name="var_8" value="def"/><br/>
<input type="text" name="multi_matrix[colors][]" value="red"/><br/>
<input type="text" name="multi_matrix[colors][]" value="blue"/><br/>
<input type="text" name="multi_matrix[colors][]" value="green"/><br/>
<input type="text" name="multi_matrix[weight][]" value="75"/><br/>
<input type="text" name="multi_matrix[weight][]" value="83"/><br/>
<input type="text" name="multi_matrix[weight][]" value="62"/><br/>
<input type="text" name="multi_matrix[height][]" value="170"/><br/>
<input type="submit" value="Send">
</form>
<style>
input {
margin: 5px 0;
}
</style>
<script type="text/javascript">
(function(){
$(function(){
$("#myForm").submit(function(event) {
var $this = $(this);
var data = $this.serialize();
$this.find("input, textarea, select, button").remove();
$this.append("<input type='hidden' class='data' name='data'/>");
$this.find("input.data").val(data);
});
});
})(jQuery);
</script>
</body>
</html>
正如您所看到的,我正在拦截提交事件并将所有表单字段替换为单个数据&#39;领域。在PHP方面,我使用explode()和pars_str()方法重新创建变量,最后我想将变量放入$ _POST数组,因此我创建了简单的递归函数,可以处理简单值和多维关联数组。 结果是$ _POST数组填充了表单值,就像表单正常提交一样。
您如何看待上述方法?也许有一个更简单的解决方案,我之前没有注意到这个问题?如果没有,有没有改进上述代码的选项?上述解决方案是否有任何负面影响?
提前致谢!