Welcome to the nuBuilder Forums!

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

Fill in current user

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Locked
mabe
Posts: 4
Joined: Tue May 28, 2019 3:03 pm

Fill in current user

Unread post by mabe »

Hi all,

I am trying to fill in the currently logged in username into a field when editing a form.
This is my JavaScript code:

Code: Select all

if(nuFormType() == 'edit') {
    var refA = #zzsys_user_id#;
    if(refA !== null) {
        $('#edited_by').val(refA).change();
    }
}
sessionStorage.removeItem("edited_by");
It works if i replace #zzsys_user_id# with a String, but I can't get the current username.

Do you have a solution to this?

Thanks very much!
kev1n
nuBuilder Team
Posts: 4305
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 446 times
Contact:

Re: Fill in current user

Unread post by kev1n »

Hi,

You can use nuUserName()
https://wiki.nubuilder.cloud/ ... nuUserName

Code: Select all

if(nuFormType() == 'edit') {
    var refA = nuUserName();
    if(refA !== null) {
        $('#edited_by').val(refA).change();
    }
}
sessionStorage.removeItem("edited_by");

But since nuUserName() returns null if logged in as globeadmin, I use my custom function:

Code: Select all

function nuUserNameEx() {
    var u = nuUserName();
    if (u == null || u == '') {
        u = "globeadmin";
    }
    return nuUser;
}

if(nuFormType() == 'edit') {
    var refA = nuUserNameEx();
    if(refA !== null) {
        $('#edited_by').val(refA).change();
    }
}
sessionStorage.removeItem("edited_by");
mabe
Posts: 4
Joined: Tue May 28, 2019 3:03 pm

Re: Fill in current user

Unread post by mabe »

kev1n wrote:Hi,

You can use nuUserName()
https://wiki.nubuilder.cloud/ ... nuUserName

Code: Select all

if(nuFormType() == 'edit') {
    var refA = nuUserName();
    if(refA !== null) {
        $('#edited_by').val(refA).change();
    }
}
sessionStorage.removeItem("edited_by");

But since nuUserName() returns null if logged in as globeadmin, I use my custom function:

Code: Select all

function nuUserNameEx() {
    var u = nuUserName();
    if (u == null || u == '') {
        u = "globeadmin";
    }
    return nuUser;
}

if(nuFormType() == 'edit') {
    var refA = nuUserNameEx();
    if(refA !== null) {
        $('#edited_by').val(refA).change();
    }
}
sessionStorage.removeItem("edited_by");
Nice!!
Thank you very much!
I had to use === for the comparison, but otherwise this works! :)
admin
Site Admin
Posts: 2815
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: Fill in current user

Unread post by admin »

.
Locked