tags:

views:

36

answers:

2

I have two tables on my page and they appear side-by-side. The first one contains an entry form and the second one contains a grid that displays rows of records.

When the user clicks on a row, the data related to that row must appear on the boxes of the form in the first table.

I know how to call a .php file through AJAX. But when I have queried the database and obtained the result-set, how to bring all those values to fill the entry form of the first table?

+1  A: 

You can get your data in json format and set input fields values in callback function using it.

$.getJSON(
  "your_url_to_request_data", {
    your_params: and_it_values
  }, 
  function(data) {
    // data <-- this is your data
    $("#field1").val( data.something );
  }
);
silent
Where to write this code?
RPK
silent: He never specified he was using jQuery. Why would you write such a non-helpfull answer?
Josua Pedersen
Josua, so you think your answer is better? :) Why do you think he is using JavaScript? He didn't specified it.
silent
+1  A: 

You need to send the result from the database to your JavaScript file via a callback function as silent implied. You also need to encode the data to JSON before sending it to JavaScript.

What you have to do in the callback function in your JavaScript file is to parse your result to JSON using native JSON parser. You access it by writing

JSON.parse(here_you_write_the_data_to_be_parsed);
Josua Pedersen
Ok, so this is what I will be doing. (1) Call a PHP file through AJAX. (2) The PHP file will create a json_encoded string. (3) The json_encoded string will be parsed back in the AJAX file.Is this what you are saying?
RPK