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?

0

1 Answer 1

1

I have figures it:

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

:)

Sign up to request clarification or add additional context in comments.

1 Comment

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.

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.