我们正在尝试从javascript / ajax向php发送多个关联数组值,但我们在php文件中收到一个空对象/数组。
那么请你帮忙从javascript到php获取关联数组的值吗?
我们尝试过序列化数组并传递给php文件。
我们尝试将数组转换为字符串,然后尝试将其传递给php文件。
请检查以下代码:
// javscript
function saveData(id) {
//var actionData = [];
var actionData = $('#' + id).closest('tr').find("[id^='action_']");
var ownerData = $('#' + id).closest('tr').find("[id^='owner_']");
//var type = $.type(actionData);
//alert(type); exit();
/*alert(actionData[0]['action']);exit;
actionData['owner'] = $('#' + id).closest('tr').find("[id^='owner_']");
actionData[2]['dueDate'] = $('#' + id).closest('tr').find("[id^='dueDate_']");
actionData[3]['completedDate'] = $('#' + id).closest('tr').find("[id^='completedDate_']");
//var myData = JSON.stringify(actionData['owner']);
//alert($('#' + id).closest('tr').find('#projId').val());*/
var dataObject = [];
dataObject['projId'] = $('#' + id).closest('tr').find('#projId').val();
//alert(myData);
//var newData1 = $.extend({}, actionData['owner']);
var newData = $.extend({}, dataObject);
/*dataObject = {
projId: $('#' + id).closest('tr').find('#projId').val(),
riskNumber: $('#' + id).closest('tr').children('td#riskNumber').text().slice(0,-4),
cause: $('#' + id).closest('tr').find('#cause').val(),
effect: $('#' + id).closest('tr').find('#effect').val(),
functionAffectedControl: $('#' + id).closest('tr').find('#functionAffectedControl :selected').val(),
categoryControl: $('#' + id).closest('tr').find('#categoryControl :selected').val(),
impact: $('#' + id).closest('tr').find('#impact :selected').val(),
severityControls: $('#' + id).closest('tr').find('#severityControls').val(),
probability: $('#' + id).closest('tr').find('#probability :selected').val(),
riskOwnerControl: $('#' + id).closest('tr').find('#riskOwnerControl :selected').val(),
manageability: $('#' + id).closest('tr').find('#manageability :selected').val(),
residualProbability: $('#' + id).closest('tr').find('#residualProbability :selected').val(),
residualImpact: $('#' + id).closest('tr').find('#residualImpact :selected').val(),
statusControl: $('#' + id).closest('tr').find('#statusControl :selected').val()
//actions: JSON.stringify(actionArray)
//owners: $('#' + id).closest('tr').find("[id^='owner_']").val(),
//dueDates: $('#' + id).closest('tr').find("[id^='dueDate_']"),
//CompletedDates: $('#' + id).closest('tr').find("[id^='completedDate_']")
}*/
$.ajax({
type: "POST",
url: "/apps/projmgmt/reports/SaveData.php",
data: { data: actionData, owner:ownerData}, //
dataType: 'json',
success: function(response) {
console.log(response);
alert('Data Saved Successfully');
}
});
}
// html
<table class="table table-bordered table-striped tree-basic">
<tbody>
{foreach from=$obj->getRiskActionList() name=risk item=risk}
<tr class="{$class}" data-count="2" id="{$k}">
{$i = $i+1}
{$class='treegrid-'|cat:$i}
{$j = $i-1}
{$classNode='treegrid-parent-'|cat:$j}
<td style="font-weight: bold;">
<div class="treegrid-container">
<span class="treegrid-expander treegrid-expander-collapsed"></span>
{'ACTION'|gettext|escape}</div>
</td>
<td colspan="6" >
<input type="hidden" name="actionId[]" id="actionId_{$k}" value="{$risk->getId()|escape}" />
<div class="treegrid-container">
<span class="treegrid-expander"></span>
<textarea rows="2" cols="100" id="action_{$k}" name="mitigationAction[]" maxlength="256">{$risk->getMitigationAction()|escape}</textarea></div>
</td>
</tr>
<tr class="{$class} {$classNode}">
{$i = $i+1}
{$class='treegrid-'|cat:$i}
<td style="font-weight: bold;" >
<div class="treegrid-container">
<span class="treegrid-expander"></span>{'OWNER_NAME'|gettext|escape}</div></td>
<td>
<input type="text" name="actionOwner[]" size="50" id="owner_{$k}" value="{$risk->getOwnerName()|escape}" maxlength="80"/>
</td>
<td style="font-weight: bold; text-align: center;" >{'DUE_DATE'|gettext|escape}</td>
<td>
<input type="hidden" class="riskPopupCalendar" id="actionDueDate_{$i}" name="actionDueDate[]" value="{$smarty.request.actionDueDate[$i]|escape}">
<div size="10" name="actionDueDateText[]" id="dueDate_{$k}">{if null != {$risk->getDueDate()|escape}}{$risk->getDueDate()->format('d/m/Y')|escape}{/if}</div>
<a href="#"><img src="/images/calendar.gif" id="actionDueDateDatepicker_{$i}" style="width:16px;height:16px;"></a>
<a href="#"><img src="/images/calendar_delete.gif" class="riskPopupCalendarDelete" style="width:16px;height:16px;"
data-input="actionDueDate_{$i}" data-text="actionDueDateText_{$i}" id="actionDueDateDelete_{$i}"></a>
</td>
<td style="font-weight: bold; text-align: center;" >{'COMPLETED_DATE'|gettext|escape}</td>
<td>
<input type="hidden" class="riskPopupCalendar" id="actionCompletedDate_{$i}" name="actionCompletedDate[]" value="{$smarty.request.actionCompletedDate[$i]|escape}">
<div size="10" name="actionCompletedDateText[]" id="completedDate_{$k}>{if null != {$risk->getCompletedDate()|escape}}{$risk->getCompletedDate()->format('d/m/Y')|escape}{/if}</div>
<a href="#"><img src="/images/calendar.gif" id="actionCompletedDateDatepicker_{$i}" style="width:16px;height:16px;"></a>
<a href="#"><img src="/images/calendar_delete.gif" class="riskPopupCalendarDelete" style="width:16px;height:16px;"
data-input="actionCompletedDate_{$i}" data-text="actionCompletedDateText_{$i}" id="actionCompletedDateDelete_{$i}"></a>
</td>
</tr>
<tr class="{$class} {$classNode}">
<div class="treegrid-container">
<span class="treegrid-expander"></span>
<td data-column="name"><img src="/images/blank.gif" class="ib_left"><input type="button" id="{$k|escape}" name="removeActionButton" value="{'REMOVE_ACTION'|gettext|escape}" class="removeActionButton ib"><img src="/images/blank.gif" class="ib_right"></td></div>
</tr>
{$i = $i+1}
{$k = $k+1}
{$class='treegrid-'|cat:$i}
{/foreach}
<tr>
<td></td><td></td><td></td><td></td><td></td>
<td align="right"><img src="/images/blank.gif" class="ib_left"><input type="button" onclick="updateData('{$row}','{$k}');" data-number="{$i}" value="{'ADD_ACTION'|gettext|escape}" class="addActionButton ib"><img src="/images/blank.gif" class="ib_right"></td>
</tr>
</tbody>
</table>
答案 0 :(得分:0)
首先确保您的数据符合您的想法:
var data = JSON.stringify({ data: actionData, owner:ownerData})
console.log(data)
如果是,则在ajax调用中发送数据对象。然后在php端我会确认收到的数据与发送的数据匹配。然后解析说的数据。这可以帮助您缩小范围并解决您的问题。