Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中将表单元素组合为字符串和还原_Php_Jquery - Fatal编程技术网

如何在PHP中将表单元素组合为字符串和还原

如何在PHP中将表单元素组合为字符串和还原,php,jquery,Php,Jquery,该网站有一个功能,让会员自定义表单内容,因此我无法控制多个表单字段,也许他们会向服务器提交1000多个表单字段,超过max\u input\u vars的限制,但表单字段应该是无限的,我不想漫无目的地扩大max\u input\u vars的数量,所以我尝试将所有字段和值组合成JSON字符串,并用PHP对其进行解码 表格 <form id="frm" action="get.php" method="POST"> <input type="text" name="name

该网站有一个功能,让会员自定义表单内容,因此我无法控制多个表单字段,也许他们会向服务器提交1000多个表单字段,超过
max\u input\u vars
的限制,但表单字段应该是无限的,我不想漫无目的地扩大
max\u input\u vars
的数量,所以我尝试将所有字段和值组合成JSON字符串,并用PHP对其进行解码

表格

<form id="frm" action="get.php" method="POST">
   <input type="text" name="name" value="chan">
   <input type="text" name="age" value="37">
   <input type="text" name="hobby" value="basketball">
   <button type="submit">go</button>
</form>
PHP

<?php

$data = json_decode($_POST['data'], true);

var_dump($data);
我不知道如何更正它。

使用表单字段的数组(集合)!当然,使用POST方法而不是GET,因为GET更有限

<form id="frm" action="action.php" method="POST">
  <input type="text" name="myfields[name]" value="chan">
  <input type="text" name="myfields[age]" value="37">
  <input type="text" name="myfields[hobby]" value="basketball">
  <button type="submit">go</button>
</form>

为什么不使用POST?这只是一个例子,我在real函数中使用了POST,让我更改代码以避免混淆您不需要序列化,只需使用POST,使参数不受web url链接的限制,并访问操作页面上的数据谢谢您的建议,但我让成员自定义表单,这意味着它们本身可能有数组元素,比如字段[content[team_information][]],它会在PHP中被切掉,更不用说如果我想这样做的话,我必须在JS和PHPY中更改太多代码。你也可以有多维表单字段,比如myfields[member][customerfields]。为什么不呢?
array(3) {
  [0]=>
  array(2) {
    ["name"]=>
    string(4) "name"
    ["value"]=>
    string(4) "chan"
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(3) "age"
    ["value"]=>
    string(2) "37"
  }
  [2]=>
  array(2) {
    ["name"]=>
    string(5) "hobby"
    ["value"]=>
    string(10) "basketball"
  }
}
<form id="frm" action="action.php" method="POST">
  <input type="text" name="myfields[name]" value="chan">
  <input type="text" name="myfields[age]" value="37">
  <input type="text" name="myfields[hobby]" value="basketball">
  <button type="submit">go</button>
</form>
$myfields=$_POST['myfields']
foreach($myfields as $key=>$val)
{
  echo $key.':'.$val.'<br>';
}
name:chan
age:37
hobby:basketball