php – Save latitude and longitude values ​​that were retrieved using JavaScript (in an HTML form) in BD

Well i think i am close to the solution but i need a new vision lol.

I have a form:






..................
Cancelar

At this point, where I have inserted several points (…….), I would like to determine the latitude and longitude of the user and send it to the database using this form.
I take latitude and longitude as follows:


This is my add () function; and so?

function add() {

    require_once('../config.php');
    require_once(DBAPI);

    if (!empty($_POST('formulario'))) {

        $formulario = $_POST('formulario');

        save($formulario);
        header('location: ../index.php');
    }
}

And the function save (); and so:

function save($data = null) {

  $database = open_database();
  $columns = null;
  $values = null;

  foreach ($data as $key => $value) {
    $columns .= trim($key, "'") . ",";
    $values .= "'$value',";
  }

  //remove a ultima virgula
  $columns = rtrim($columns, ',');
  $values = rtrim($values, ',');

  $sql = "INSERT INTO formularios ($columns)" . " VALUES " . "($values);";

  try {
    $database->query($sql);

    $_SESSION('message') = 'Formulario cadastrado com sucesso.';
    $_SESSION('type') = 'success';

  } catch (Exception $e) { 

    $_SESSION('message') = 'Nao foi possivel realizar a operacao.';
    $_SESSION('type') = 'danger';
  } 

  close_database($database);
}

I think it's about passing the LATITUDE and LONGITUDE variables from the script in the middle of the form to the variables form (& # 39; width & # 39;) and form (& # 39; length & # 39;), but I don't know and I don't think how I can do that.
I have already seen that the latitude and longitude recording function works. I managed to display the values ​​on the screen and the browser asks if I want to grant access to my location.

Thanks in advance for your help.