Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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
});
share|improve this answer
    
Cool thank I will try this –  Tom Hanson Feb 17 '14 at 4:51
    
Thank you this worked a charm :D :D –  Tom Hanson Feb 17 '14 at 4:52
    
@TomHanson you're welcome.. Btw, you could mark it as Accepted Answer if it worked for you :) –  DemoUser Feb 17 '14 at 5:18

First Set the datatype as JSON

$.post("php/getAccount.php", {ID: accountID}, function(data)
{

    // Confirm Response
    console.log(data);
    $.each(data, function(i, e){
       console.log(e);
    });
}, 'json');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.