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 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!