Calling a function just after

What is this best way to call a function that returns a value or does something immediately after the save event. In my example below I wanted to save the data from the form and then I call a function to retrieve the newly created id. On its own the function works as expected. Put into the attachment code below I get value undefined.

Any help would be appreciated.

generatorForm.attachEvent("onButtonClick", function (btnName) { // save or cancel if (btnName == "cancel") { callbacks.clearDashboard(); } else if (generatorForm.validate()) {; console.log(LastAddedId("generators")); } });
my function:

LastAddedId = function(tble){

dhx.ajax().get("data/lastadded.php?id="+tble, function(text,xml){
    var data = dhx.DataDriver.json.toObject(text,xml);

    data =["0"].id;
    return data;


Please, provide us completed demo on with link to this topic … pport.html
We will inspect it localy

Hi Darya

i’m pretty sure my problem is from not using callbacks correctly. In the demo I have a function that will return the last added ID from a table. I just wanted this available at the time of save on the form to perform other functions. I think the problem is that the value hasn’t been calculated yet and so I would need to wait. (661 KB)


seems like you need the following:

var dp = new dataProcessor("data/formGenerators.php"); dp.init(form); dp.attachEvent("onAfterUpdate", function(id, action, tid, response){ if (action == "inserted") { console.log(tid); } });

instead of [ function getLastAddedId(tble) ]

here is documentation … event.html

That worked great. I had no idea i could retrieve the ID this way. I know its documented, but, I guess you have so many great features that it is day for us to over look.

This has solved my problem for now as I don’t have to wait for a php query to retrieve the last ID.

I know it might be asking a lot, but, I’m new to javascript and am learning it because I like DHTMLX. I have several functions that Id like to implement which are similar to my old solution, how would I best structure my code to make it work? i.e. wait for a function then return the result to another.

Thanks again


there are lot of ways to call functions in ‘async world’, all depending on certain case. for example you may be interested in Promise, here is an article from google_search

I’ll check it out.
Thanks Andrei