tags:

views:

41

answers:

1

Using jQuery I get JSON object that extracts one row from database. That row has about 30 columns so along with row data, success message (success = true) JSON result returns array with table columns (ex: id, name, address, etc) called table_columns.

The purpose is to place all the returned data into the relevant html form fields (for example: address input will be filled with data.adress) and I would like to descover the trick that lets loop through data.table_columns array and stores json data into its corresponding form inputs. Let me show you a code to explain it better:

$.getJSON(base_url+'index.php/iprdb/ajax_get_row/'+entry_id, function(data){
    if(!data.success) {
        alert('server error. please try again or contact support');    
    } else {
         $.each(data.table_columns, function(i, item) {
             var myVar = item; // gets table column name ex: address
             $('#'+myVar).val(data.myVar); // stores address into input with id #address
         });                    
    }    
});

so is it possible to access data.address for example using this method?

+1  A: 

I have figures it:

var myVar = item;
$('#'+myVar).val(data[myVar]);

:)

Mike
good job :), and you don't need the `myVar` there since it's the same as `item`, you can directly write `data[item]`. Don't forget you select this as the correct answer by ticking the check mark.
Anurag
ahh beat me to it
Darcy