0

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 Answers 2

1

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
  • @TomHanson you're welcome.. Btw, you could mark it as Accepted Answer if it worked for you :) Commented Feb 17, 2014 at 5:18
0

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');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.