Fill in current user

Questions related to customising nuBuilder Forte with Javascript or PHP.

Fill in current user

Postby mabe » Wed Jun 05, 2019 6:48 pm

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!
mabe
 
Posts: 4
Joined: Tue May 28, 2019 10:33 pm

Re: Fill in current user

Postby kev1n » Wed Jun 05, 2019 7:02 pm

Hi,

You can use nuUserName()
https://wiki.nubuilder.net/nubuilderfor ... 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");
kev1n
 
Posts: 185
Joined: Mon Oct 15, 2018 2:13 am

Re: Fill in current user

Postby mabe » Wed Jun 05, 2019 7:20 pm

kev1n wrote:Hi,

You can use nuUserName()
https://wiki.nubuilder.net/nubuilderfor ... 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! :)
mabe
 
Posts: 4
Joined: Tue May 28, 2019 10:33 pm

Re: Fill in current user

Postby admin » Fri Jun 07, 2019 5:33 pm

.
admin
Site Admin
 
Posts: 3070
Joined: Mon Jun 15, 2009 9:53 am


Return to Custom Code



cron