This topic is locked

Calculate the field value of one table based on the field value of another table on the fly

4/28/2023 5:34:59 AM
PHPRunner General questions
A
alfonso authorDevClub member

I have read this article to calculate fields based on the value of other fields.
https://xlinesoft.com/articles/how_to_use_calculated_fields.htm
This code works fine in Events -> Before record added
$values["kms"] = DBLookup("SELECT km FROM rutas WHERE id_ruta = ".$values["ruta_id"]);
$values["preciototal"] = DBLookup("SELECT precio FROM servicios WHERE id_servicio = ".$values["servicio_id"]) * $values["kms"];
As you can see, it updates a field called kms based on the value of another field in another table (it is of the Lookup wizard type).
What I want is for that value to be seen directly when a new record is being edited or added "on the fly". I think I have to put the code in "Javascript onload event", but I don't know exactly how to do it
Any idea? Thanks

fhumanes 4/30/2023

Hi @alfonso,
One of the alternatives you could use is to make an Ajax request, passing the data from the browser (JavaScript) to the server (PHP), to calculate and save this in the field of the browser you want.
Check this article that explains this type of requests.

Greetings,
fernando