A special type of field that allows you to perform calculations on the fly in a record form. In the "PHP code" field, enter your own code that will be executed when opening the form. If the code uses values from the form, then when the value in the input field is changed, the code will be executed again.

Working with values

To get a value from the form, use the following construction: [field id]. For example: $distance = [379];
For a nested entity, you can get the value from the parent record in the same way.

Note: if the values of field 379 are empty, then $distance = 0;.

To get the id of the current record, use $item_id = $_GET['item_id'];
$_POST['parent_item_id'] - id of the parent record

Use the "Debug mode" option to check the code that will be executed.

If the default text is entered, it will be displayed when an ajax request returns an empty response.

Output and save the value in the database

Use the echo  command to output the value. The output can be in any format, such as echo '<b style= "color:red">' . $value . '</b>';

To save the value in the database, assign the value to a special variable $form_field_value, for example $form_field_value = $value;

JavaScript

If you need to assign the received value to another field in the form, you can do this using JavaScript code. Example:

echo '<script>$("#fields_13").val(' . $value . ')</script>';

Where 13 is the field id. $value - the required value.

MySql query

Using the db_query() function, you can build a query to the required table and select the values you need. For example:

$tariff_query = db_query("select field_389 from app_entity_35 where " . $distance . " BETWEEN field_387 and field_388",false);

Full Example

$distance = [379]; //Get distance value from current form

if($distance>0)
{
  //create MySql query
  $tariff_query = db_query("select field_389 from app_entity_35 where " . $distance . " BETWEEN field_387 and field_388",false);

  //if found row then do calculation
  if($tariff = db_fetch_array($tariff_query))
  {
    $value = $distance*$tariff['field_389']; //get the value
    echo '<b style="color:red">' . $value . '</b>'; //output value
    $form_field_value = $value;   //value to save in database
  }
}