dongxian8048 2014-02-17 04:46
浏览 49
已采纳

Php echo json_encode数组进入javascript数组

I am trying to send a post to a php script which will collect all the account information from the database using this...

var e = document.getElementById("lstAccounts");
var accountID = e.options[e.selectedIndex].value;
alert("Account ID:"+accountID);
$.post("php/getAccount.php", {ID: accountID}, function(data)
{
    var accountInfo = data;
});

This posts into this...

<?php
$id = $_POST['ID'];
include('database_api.php');
$db = new DatabaseControl;
$db->open_connection();
$result = $db->db_query("SELECT * FROM tblAccount WHERE ID=$id");
$account_info = array();
//Get Basic Information
while($row = mysqli_fetch_array($result))
{
    $account_info['Name'] = $row['Name'];
    $account_info['CRN'] = $row['CRN'];
    $account_info['ID'] = $row['ID'];
    $account_info['Type'] = $row['Type'];
    $account_info['Revenue'] = $row['Revenue'];
    $account_info['Industry'] = $row['Industry'];
    $account_info['Description'] = $row['Description'];
    $account_info['Employees'] = $row['NoOfEmployees'];
    $account_info['Billing'] = $row['BillingAddress'];
    $account_info['Shipping'] = $row['ShippingAddress'];
}
//Get Details
$result = $db->db_query("SELECT tblDetails.ID, tblDetails.Label, tblDetails.Value FROM tblAccountDetails
                            INNER JOIN tblDetails ON tblDetails.ID = tblAccountDetails.DetailID
                            WHERE AccountID=$id");
//Get Basic Information
while($row = mysqli_fetch_array($result))
{
    $account_info['Detail'.$row['ID']]['Label'] = $row['Label'];
    $account_info['Detail'.$row['ID']]['Value'] = $row['Value'];
}
//Get Contact Information

//Get Invoices

//Get Payments

//Get Notes

//Get To-Do

//Events

//Send back to javascript
echo json_encode($account_info);
?>

I need the echoed json_encode to enter a javascript on the return data. How do I get that data into an array?

$.post("php/getAccount.php", {ID: accountID}, function(data)
{
    //In here how do I decode data into a javascript array
});

The data is set at "{"Name":"A business name","CRN":null,"ID":"17","Type":"User","Revenue":null,"Industry":"Software & Internet","Description":null,"Employees":null,"Billing":"An Address","Shipping":"An Address","Detail75":{"Label":"Phone","Value":"a phone number"},"Detail76":{"Label":"Email","Value":"an email address"}}" on return

  • 写回答

2条回答 默认 最新

  • dounan9070 2014-02-17 04:47
    关注

    pass in json_encode()'ed data from your php, like:

    ...
    while($row = mysqli_fetch_array($result))
    {
        $account_info['Detail'.$row['ID']]['Label'] = $row['Label'];
        $account_info['Detail'.$row['ID']]['Value'] = $row['Value'];
    }
    echo json_encode($account_info);
    

    in js part:

    $.post("php/getAccount.php", {ID: accountID}, function(data) {
        //parse the json response
        var response = jQuery.parseJSON(data);
        console.log(response); //you can use $.each to iterate the data
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行