簡體   English   中英

需要將數組從 PHP 發送到 JavaScript

[英]Need To Send Array From PHP To JavaScript

我正在使用一個 crud 應用程序,如果我不在我的 PHP 中添加(子數組)部分,它工作正常,但我需要在解密后將(子數組)從 PHP 傳遞到 JavaScript 代碼。

但我收到此錯誤:

(DataTables 警告:表 id=tablaUs - 請求的第 189 行第 0 列的未知參數“ID”。有關此錯誤的更多信息,請參閱http://datatables.net/tn/4

它顯示相同的數據未解密,並且還顯示了很多空白行。

我的 PHP 代碼:

<?php
case 4: // Display All Users
 
        $sql = "SELECT * FROM structure";
        $result = $con1->prepare($sql);
        $result->execute();        
        $data=$result->fetchAll(PDO::FETCH_ASSOC);

//// Sub Array Part

        foreach($data as $row)
        {
         $sub_array = array();
         $sub_array[] = decryptthis($row['ID'], $Key);
         $sub_array[] = decryptthis($row['U_ID'], $Key);
         $sub_array[] = decryptthis($row['Username'], $Key);
         $sub_array[] = decryptthis($row['ECRM_Name'], $Key);
         $sub_array[] = $row['EBU_Title'];
         $sub_array[] = $row['Market_Segment'];
         $sub_array[] = $row['Unit_Manager'];
         $sub_array[] = $row['Division_Manager'];
         $sub_array[] = $row['Customer_Type'];
         $sub_array[] = decryptthis($row['Phone_Number1'], $Key);
         $sub_array[] = decryptthis($row['E_Mail'], $Key);
         $sub_array[] = $row['Joining_Date'];
         $sub_array[] = $row['Current_Status'];
         $data[] = $sub_array;
        }
////
        break;
        
}

print json_encode($data, JSON_UNESCAPED_UNICODE);
$con1=null;
?>

這是我的 JavaScript 代碼:

$(document).ready(function() {

var ID, option;
option = 4;
    
tablaUs = $('#tablaUs').DataTable({ 

 "ajax":{            
        "url": "bd/crud.php", 
        "method": 'POST',
        "data":{option:option},
        "dataSrc":""
    },
    "columns":[
        {"data": "ID"},
        {"data": "U_ID"},
        {"data": "Username"},
        {"data": "ECRM_Name"},
        {"data": "EBU_Title"},
        {"data": "Market_Segment"},
        {"data": "Unit_Manager"},
        {"data": "Division_Manager"},
        {"data": "Customer_Type"},
        {"data": "Phone_Number1"},
        {"data": "E_Mail"},
        {"data": "Joining_Date"},
        {"data": "Current_Status"},     
        {"defaultContent": "<div class='text-center'><div class='btn-group btn-group-sm'><button class='btn btn-info btnEdit'><i class='fas fa-pen'></i></button><button class='btn btn-danger btnDelete'><i class='fas fa-trash'></i></button></div></div>"}
    ],
}); 

嘗試將您在數據中定義的相同鍵添加到subarray

像這樣

$sub_array['ID'] = decryptthis($row['ID'], $Key);
...

謝謝大家,您的評論讓我進行了一些搜索,並且在進行了一些修改后它起作用了:

case 4: // 顯示所有用戶

    $sql = "SELECT * FROM structure";
    $statement = $con1->prepare($sql);
    $statement->execute();        
    $result=$statement->fetchAll(PDO::FETCH_ASSOC);
    
    $data = array();
    
    foreach($result as $row)
    {
    
    $U_ID= $row['ID'];  
    $U_ID= decryptthis($row['U_ID'], $Key);
    $Username= decryptthis($row['Username'], $Key);
    $ECRM_Name= decryptthis($row['ECRM_Name'], $Key);
    $EBU_Title= $row['EBU_Title'];
    $Market_Segment= $row['Market_Segment'];
    $Unit_Manager= $row['Unit_Manager'];
    $Division_Manager= $row['Division_Manager'];
    $Customer_Type= $row['Customer_Type'];
    $Phone_Number1= decryptthis($row['Phone_Number1'], $Key);
    $E_Mail= decryptthis($row['E_Mail'], $Key); 
    $Joining_Date= $row['Joining_Date'];
    $Current_Status= $row['Current_Status'];

     $sub_array = array();
     $sub_array['ID'] = $U_ID;
     $sub_array['U_ID'] = $U_ID;
     $sub_array['Username'] = $Username;
     $sub_array['ECRM_Name'] = $ECRM_Name;
     $sub_array['EBU_Title'] = $EBU_Title;
     $sub_array['Market_Segment'] = $Market_Segment;
     $sub_array['Unit_Manager'] = $Unit_Manager;
     $sub_array['Division_Manager'] = $Division_Manager;
     $sub_array['Customer_Type'] = $Customer_Type;
     $sub_array['Phone_Number1'] = $Phone_Number1;
     $sub_array['E_Mail'] = $E_Mail;
     $sub_array['Joining_Date'] = $Joining_Date;
     $sub_array['Current_Status'] = $Current_Status;
     $data[] = $sub_array;
    }

    break;
    

}

print json_encode($data, JSON_UNESCAPED_UNICODE);//envio el array final el formato json a AJAX $con1=null;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM