简体   繁体   中英

Need To Send Array From PHP To JavaScript

I'm using a crud application, it works fine if I don't add the (sub-array) part in my PHP, but I need to pass the (sub-arrays) from the PHP to the JavaScript code after decrypting them.

But I receive this error:

(DataTables warning: table id=tablaUs - Requested unknown parameter 'ID' for row 189, column 0. For more information about this error, please see http://datatables.net/tn/4 )

And it shows the same data not decrypted, and a lot of blank rows show up too.

My PHP Code:

<?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;
?>

And this is my JavaScript code:

$(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>"}
    ],
}); 

try to add the same keys you have defined in you data to your subarray

like this

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

thanks guys your comments made me do some searching and it worked, after making some modifications:

case 4: // Display All Users

    $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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM