Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

logged in user info Topic is solved

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
Costa
Posts: 39
Joined: Sun Aug 15, 2021 9:46 am
Has thanked: 11 times
Been thanked: 4 times

logged in user info

Unread post 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
kev1n
nuBuilder Team
Posts: 4291
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: logged in user info

Unread post 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.
Costa
Posts: 39
Joined: Sun Aug 15, 2021 9:46 am
Has thanked: 11 times
Been thanked: 4 times

Re: logged in user info

Unread post 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
Costa
Posts: 39
Joined: Sun Aug 15, 2021 9:46 am
Has thanked: 11 times
Been thanked: 4 times

Re: logged in user info

Unread post 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
kev1n
nuBuilder Team
Posts: 4291
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: logged in user info

Unread post 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());
Costa
Posts: 39
Joined: Sun Aug 15, 2021 9:46 am
Has thanked: 11 times
Been thanked: 4 times

Re: logged in user info

Unread post by Costa »

Hi Kev1n,

Always simple and top solutions!

Thanks
Costa
Costa
Posts: 39
Joined: Sun Aug 15, 2021 9:46 am
Has thanked: 11 times
Been thanked: 4 times

Re: logged in user info

Unread post 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
kev1n
nuBuilder Team
Posts: 4291
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

Re: logged in user info

Unread post 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.
Costa
Posts: 39
Joined: Sun Aug 15, 2021 9:46 am
Has thanked: 11 times
Been thanked: 4 times

Re: logged in user info

Unread post 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!
Post Reply