2017-09-25 87 views
0

我們試圖從javascript/ajax發送多個關聯數組值到php,但我們在php文件中收到一個空的對象/數組。無法從javascript/ajax發送多個關聯數組值到php

所以,你可以請幫忙從JavaScript到PHP的關聯數組的值?

  1. 我們嘗試了序列化數組並傳遞給一個php文件。

  2. 我們試圖將數組轉換爲字符串,然後嘗試將它傳遞給一個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

首先確保你的數據是什麼,你認爲它是:

var data = JSON.stringify({ data: actionData, owner:ownerData}) 
console.log(data) 

如果然後在Ajax調用發送的數據對象。然後在PHP方面,我會確認收到的數據與發送的數據相匹配。然後解析所述數據。這應該有助於縮小範圍並解決您的問題。