简体   繁体   English

JQUERY:如何序列化具有相同名称的输入字段

[英]JQUERY: how to serialize input fields with same name

I have the following form that has lots of similar type of input fields with same name (eg. 10 fields for 'name', 10 fields for 'address'). 我有以下形式的表单,其中有许多相似类型的输入字段具有相同的名称(例如,“名称”为10个字段,“地址”为10个字段)。 How many times these input fields will repeated, can not be said in prior and therefore they cannot be given static different names (eg. 'name1', 'name2', 'address1', 'address2'). 这些输入字段将重复多少次,无法事先说明,因此无法为它们指定静态的不同名称(例如,“名称1”,“名称2”,“地址1”,“地址2”)。

Problem: while I am posting data using ajax post (serialized), its only posting the first value of fields with same name (received with php). 问题:当我使用ajax post(序列化)发布数据时,它仅发布具有相同名称的字段的第一个值(使用php接收)。

Required: 需要:

  1. How can I get all the input data posted properly? 如何正确输入所有输入数据?
  2. Whats the best way to name such input fields that contain similar data for the purpose of catching those data with php (form is generated in php)? 命名此类包含相似数据的输入字段的最佳方法是什么,以便用php捕获这些数据(表单是在php中生成的)?

Sample code: 样例代码:

    <form name="content">
     <table>
      <tr>
       <td>
        <input name="full_name" type="text" />
       </td>
       <td>
        <input name="address" type="text" />
       </td>
      </tr>
      <tr>
       <td>
        <input name="full_name" type="text" />
       </td>
       <td>
        <input name="address" type="text" />
       </td>
      </tr>
    </table>
   </form>

I think in your case you can use $.serializeArray() : 我认为在您的情况下,您可以使用$.serializeArray()

var data = $('form[name="content"]').serializeArray();

this will produce something like this: 这将产生如下内容:

data = [
     {
       name : "full_name",
       value : "thefieldvalue"
     },
     {
       name : "address",
       value : "theaddressvalue"
     },
     .....
];

See this: 看到这个:

data:$('form[name="content"]').serializeArray()+'&request=insert_invoice' 

not a correct way to send data instead you can try with this below: 不是正确的数据发送方式,您可以尝试以下操作:

data:{
    frmvalues : $('form[name="content"]').serializeArray(), 
    request:insert_invoice
} 
<input name="full_name[]" type="text" value="foo" />
<input name="full_name[]" type="text" value="bar" />

In PHP it will be: 在PHP中它将是:

Array (
    full_name => Array (
         0 => foo
         1 => bar
    )
)

You have to serialize the data and send it through ajax. 您必须序列化数据并通过ajax发送。 On the php side unserialize the data and format it through this function to get the output described the comment above mine. 在php方面,反序列化数据并通过此函数将其格式化以获取输出,该输出描述了我上面的注释。 Without it, it will won't return the desired output. 没有它,它将不会返回所需的输出。

 public function serializedFormDatajQuery2Array($serializedArr){
                  $aFormData = array();
                  foreach($serializedArr as $aRow){

                     if(isset($aFormData[$aRow['name']]) && !is_array($aFormData[$aRow['name']])){
                        $sValue = $aFormData[$aRow['name']];
                        $aFormData[$aRow['name']] = array();
                        $aFormData[$aRow['name']][] = $sValue;
                        $aFormData[$aRow['name']][] = $aRow['value'];
                        continue;
                     }

                                if(is_array($aFormData[$aRow['name']])){
                                            $aFormData[$aRow['name']][] = $sValue;
                                            continue;
                                }

                  $aFormData[$aRow['name']] = $aRow['value'];
                  }
                             return $aFormData;
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM