testfrm1.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<form id="form1" name="form1" method="post">
<table>
<thead>
<tr class="block_sortlines">
<th class="req" width="15%">Problem</th>
<th class="req" width="15%">Location</th>
<th width="12%">Priority</th>
<th style="width:10%;"class="action"></th>
</tr>
</thead>
<tbody>
<tr id="maintenance_row_1">
<td> New Problem </td>
<td> balcony </td>
<td> high </td>
<td> <a class="action button right js-edit-subtask-1" onClick="editSubtask(1);">Edit Sub</a> </td>
<input type="hidden" name="sub_maintenance_template[1][maintenance_location_id]" value="12345" />
<input type="hidden" name="sub_maintenance_template[1][maintenance_problem_id]" value="13456" />
<input type="hidden" name="sub_maintenance_template[1][maintenance_priority_id]" value="56436" />
</tr>
<tr id="maintenance_row_2">
<td> New Problem1 </td>
<td> balcony1 </td>
<td> low </td>
<td> <a href="" class="action button right js-edit-subtask-2">Edit Sub</a> </td>
<input type="hidden" name="sub_maintenance_template[2][maintenance_location_id]" value="1111" />
<input type="hidden" name="sub_maintenance_template[2][maintenance_problem_id]" value="2223" />
<input type="hidden" name="sub_maintenance_template[2][maintenance_priority_id]" value="23523" />
</tr>
<tr id="maintenance_row_3">
<td> prob1 </td>
<td> bathroom </td>
<td> test </td>
<td> <a class="action button right">Edit Sub</a> </td>
<input type="hidden" name="sub_maintenance_template[3][maintenance_location_id]" value="54321" />
<input type="hidden" name="sub_maintenance_template[3][maintenance_problem_id]" value="65432" />
<input type="hidden" name="sub_maintenance_template[3][maintenance_priority_id]" value="76896" />
</tr>
</tbody>
<tbody>
<h4 class="left">Sub-tasks</h4>
<a href="" class="action button right js-add-subtask"><i class="add"></i> Sub-task</a>
</tbody>
</table>
</form>
<script>
function editSubtask( intSubTaskId ){
alert( intSubTaskId);
subMaintenanceTemplateRow = $('#maintenance_row_'+intSubTaskId);
$.ajax({
type: "POST",
url: "testFrm2.php",
data: {"pTableData" : subMaintenanceTemplateRow.serialize()}
});
}
</script>
</body>
testFrm2.php
<?php
$tableData = stripcslashes($_POST['pTableData']);
$tableData = json_decode($tableData,TRUE);
print_r( $tableData);
?>
testFrm1.php是基本形式。点击编辑按钮调用testFrm2.php。 我试图在testFrm2.php上打印内容,但它什么都没打印。对此有何帮助? $ .ajax方法可能有问题。将表行的数据传递给$ .ajax方法。
答案 0 :(得分:2)
您正在错误的选择器上应用serialize()
方法,您在PHP中处理提交的数据的方式也是不正确的。有两种方法可以解决这个问题:
方式(1):强>
$.ajax({
...
data: {"pTableData" : JSON.stringify(subMaintenanceTemplateRow.children('input').serializeArray())},
success: function(data){
alert(data);
}
});
使用serializeArray()
方法将输入元素编码为名称和值数组,然后使用JSON.stringify()
将数组转换为JSON字符串。这样您就不必更改PHP代码中的任何内容。
<?php
$tableData = stripcslashes($_POST['pTableData']);
$tableData = json_decode($tableData,TRUE);
echo '<pre>'; print_r($tableData);
?>
方式(2):强>
$.ajax({
...
data: {"pTableData" : subMaintenanceTemplateRow.children('input').serializeArray()},
success: function(data){
alert(data);
}
});
使用serializeArray()
方法将输入元素编码为名称和值数组,并直接将其发送到 testFrm2.php 页面。这样你就不必解码任何东西,只需像这样访问数组:
<?php
echo '<pre>'; print_r($_POST['pTableData']);
?>
根据your requirement below,我可以获得这样的数组 - [sub_maintenance_template] => Array ( [3] => ( [maintenance_location_id]=> 54321 [maintenance_problem_id]=> 65432 [maintenance_priority_id]=>76896 [description]=> sample description ) )
,更改 testFrm2.php 页面中的后端PHP代码以下方式,
<?php
$resultArr = array(
'sub_maintenance_template' => array(
'maintenance_location_id' => $_POST['pTableData'][0]['value'],
'maintenance_problem_id' => $_POST['pTableData'][1]['value'],
'maintenance_priority_id' => $_POST['pTableData'][2]['value'],
'description' => 'your sample descrition'
)
);
echo '<pre>'; print_r($resultArr);
?>