populate value of a field from another table

populate value of a field from another table

amir7shamir7sh Posts: 15Questions: 4Answers: 0

Hello friends

I want populate value of a field from another table use jquery $.ajax method
code :

{ data: null,
render : function ( data, type, full, meta ) {
$.ajax({
            type: "POST", 
                    url: "funcs.php", 
                    data: {id : data.id},
                    dataType: "json", 
                    processdata: true,
            cache: false,                   
                    success: function (json) {
                        result = json;
                    }
                    });
                  return result;
                }
            }

But the code did not work and I saw this error :
ReferenceError: result is not defined .

Where is the problem?

Replies

  • tangerinetangerine Posts: 3,342Questions: 35Answers: 394

    "result" is only defined in your "success" function. No success - no result - result is undefined where you try to return it.

  • allanallan Posts: 61,452Questions: 1Answers: 10,055 Site admin

    You need to recall what the first letter of Ajax means - Asynchronous. You could use async in the jQuery options list to disable that, but I would suggest that is an extremely bad idea - it will kill performance and you'll DDoS your own server.

    I would suggest rethinking what you are actually trying to do.

    Allan

  • amir7shamir7sh Posts: 15Questions: 4Answers: 0
    edited September 2015

    Thank Allan , What's the right way to do it?

    I want to have the total price from the other table .

  • amir7shamir7sh Posts: 15Questions: 4Answers: 0

    OK tangerine , What is the correct method?

  • allanallan Posts: 61,452Questions: 1Answers: 10,055 Site admin

    I would suggest that you should modify the data that is returned from the server (assuming you are ajax loading the data - there is no way of knowing without a test case) to include all of the information required. It will be far faster doing that that it will be making 4 * num_rows calls to the server - where num_rows is the number of rows of course (because that is what your code above would be doing).

    Allan

This discussion has been closed.