Page 1 of 1

logged in user info

Posted: Mon Mar 31, 2025 10:43 pm
by Costa
hello everyone,
I have a form with a field that I populate with this js with the id of the logged in user.

Code: Select all

if (nuFormType() == 'edit') {
     $('#pre_id_utente').val(nuCurrentProperties().user_id).change();

I tried similarly to populate other fields with additional information of the logged in user (name, login name etc) but I failed. Any suggestions on how to proceed?

Thanks for any help
Costa

Re: logged in user info

Posted: Tue Apr 01, 2025 5:54 am
by kev1n
Hi Costa,

Could you provide the full code you’re using to populate the other fields? It would help to see what you've tried so far and where it might be going wrong.

Re: logged in user info

Posted: Tue Apr 01, 2025 9:14 am
by Costa
Hi Kev1n,
nice to read from you!

I tried different ways and I also asked chatgpt who suggested using ajax with this js

Code: Select all

if (nuFormType() == 'edit') {
  // Ottieni l'ID utente loggato
  let userId = nuCurrentProperties().user_id;

  // Popola il campo pre_id_utente
  $('#pre_id_utente').val(userId).change();

  // Costruisci i dati per la query
  let requestData = {
    nuSQL: "SELECT sus_first_name FROM zzzzsys_user WHERE zzzzsys_user_id = '" + userId + "'"
  };

  // Chiamata AJAX per ottenere il sus_first_name
  $.ajax({
    type: "POST",
    url: "nuajaxPHP.php",
    data: requestData,
    success: function(response) {
      if (response && response[0] && response[0].sus_first_name) {
        $('#pre_nome_utente').val(response[0].sus_first_name).change();
      } else {
        console.log("Nessun nome trovato per l'utente con ID:", userId);
      }
    },
    error: function(xhr, status, error) {
      console.error("Errore nella chiamata AJAX:", error);
    }
  });
}

but that doesn't work in fact it doesn't even populate the user_id field because it doesnt find the url and i didnt find any ajax style .php file in the installation.

I tried using a query changing the field type to select but it gives undefined.

Maybe php could be the way but it is not in my capabilities and then I believe that it is necessary that the value of the user_id must already be written in the db field which is not true when filling out the form where this data must be exposed

Re: logged in user info

Posted: Tue Apr 01, 2025 9:56 am
by Costa
Hi kev1n,

i made a step forward, i found this file /core/libs/nudb/libraries/classes/Import/Ajax.php and i added in the url seems working without error but the replay is empy and than i get from the console "Nessun nome trovato per l'utente con ID: 67e91834294f44a" that means "no name found for user ID 67e91834294f44a

Re: logged in user info

Posted: Wed Apr 02, 2025 2:13 am
by kev1n
Ajax.php is a library that belongs to phpMyAdmin and can't be used to access nuBuilder properties.
Just use nuUserFirstName():

Code: Select all

nuSetValue('pre_nome_utente', nuUserFirstName());
And instead of

Code: Select all

$('#pre_id_utente').val(nuCurrentProperties().user_id).change();
This is shorter:

Code: Select all

nuSetValue('pre_id_utente', nuUserId());

Re: logged in user info

Posted: Wed Apr 02, 2025 11:35 am
by Costa
Hi Kev1n,

Always simple and top solutions!

Thanks
Costa

Re: logged in user info

Posted: Wed Apr 02, 2025 2:33 pm
by Costa
Kev1n,

I'm sorry to bother you but something strange happens. This is the js code that I inserted in the browse & edit section of the form following your suggestions.

Code: Select all

if (nuFormType() == 'edit') {
	nuSetValue('pre_id_utente', nuUserId());
	nuSetValue('pre_nome_utente', nuUserFirstName());
	nuSetValue('pre_cognome_utente', nuUserLastName());
	nuSetValue('pre_login_name', nuUserLogin());
}
When I open the edit form all the fields are perfectly filled with all informations but once I enter the other informations in others fields and save the file, the file is saved regularly with all the information but the save button remains red and if I try to close the form it tells me "Leave this form without saving?" But as said, if I check, all the information has been inserted into the db. If I remove the code this does not happen even if it does not populate the related fields.

Any suggestion?

Thanks for your support

Re: logged in user info

Posted: Wed Apr 02, 2025 5:21 pm
by kev1n
The described behavior occurs because the fields are populated when the form is displayed.
After saving, the form reloads, and the fields are populated again. As a result, the form is in an unsaved state, and the save button turns red again.

One possible solution is to populate the fields only before saving. The nuBeforeSave() function would be a suitable place for this.

Re: logged in user info

Posted: Wed Apr 02, 2025 5:38 pm
by Costa
Perfect!

this is the final code, for anyone who needs it.

Code: Select all

function nuBeforeSave() {
    nuSetValue('pre_id_utente', nuUserId());
    nuSetValue('pre_nome_utente', nuUserFirstName());
    nuSetValue('pre_cognome_utente', nuUserLastName());
    nuSetValue('pre_login_name', nuUserLogin());

    return true;
}

Thanks Kev1n!