Jquery POST标签/值对


Jquery POST label/value pair

我现在使用jquery.submit操作(表单)来POST数据。JAVA中的正常POST操作不会传递标签/值密钥。对于下面的(示例)表格,我是如何做到的?。我不想使用Ajax,因为我的表单也会有上传文件字段,我只知道如何在简单的POST操作中处理它。

<body>
     <form id="frmRequest" name="frmRequest" >
                <div class="clearfix" id="idRequestDetails"  >
                    <table width="809" border="0" id="tbl_data_1_1_1_1__" summary="Profile">
                      <tr>
                        <th width="156" scope="col"><label class="labelrequest" for="txtProfileName1__">Name</label>
                        </th>
                        <th width="74" scope="col"><label class="labelrequest" for="txtProfileUserID1__">User ID</label></th>
                        <th width="131" scope="col"><label class="labelrequest" for="txtSiteCost1__">Site Cost Centre</label></th>
                         <th width="182" scope="col"><label class="labelrequest" for="txtDetail1__">Additional Details</label></th>
                      </tr>
                      <tr>
                        <td><input type="text" name="txtProfileName1__" id="txtProfileName1__" tabindex="100" /></td>
                        <td><input name="txtProfileUserID1__" type="text" class="clearfix" id="txtProfileUserID1__" tabindex="110" size="8" /></td>
                        <td><input name="txtSiteCost1__" type="text" id="txtSiteCost1__" tabindex="220" size="8" /></td>
                        <td><textarea name="txtDetail1__" rows="1" id="txtDetail1__" tabindex="240"></textarea></td>
                      </tr>
                    </table>
                  </div>
        </body>

尝试了以下操作,但不工作

foreach ($_POST  as $key => $value)
{
   if($key === 'labels') {
      // Decode JSON string to array
      $value = json_decode($value, true);
   }
   if (!is_array($value))
   {
      $message .= "<br/>".$key." : ".$value;
   }
   else
   {
      foreach ($value as $itemvalue)
      {
         $message .= "<br/>".$value." : ".$itemvalue;
      }
   }
} 

在发送表单之前,您必须使用所需的信息构建输入。

这是我在LABEL标签之间找到文本的示例。创建隐藏的INPUT。设置INPUT的名称。设置INPUT的值,该值是标签值的JSON编码数组

$("form").submit(function(event) {
   $labels = $(this).find("label");
   $ret = [];
   $.each($labels, function() {
       $ret.push($(this).text());
   });
   $input = $("<input>").attr("type", "hidden").attr("name", "labels").val(JSON.stringify($ret));
   $(this).append($input);
});

由于我将所有标签放入JSON对象中,在foreach循环中,在第一个if之前,您需要找到标签数据,将其解码为数组

像这样:

foreach ($_POST  as $key => $value)
{
   if($key === 'labels') {
      // Decode JSON string to array
      $value = json_decode($value, true);
   }
   if (!is_array($value))
   {
      $message .= "<br/>".$key." : ".$value;
   }
   else
   {
      foreach ($value as $itemvalue)
      {
         $message .= "<br/>".$value." : ".$itemvalue;
      }
   }
} 

以防万一,请查看JSON函数和您的PHP版本