Page 1 of 1

Fill fields On Save

Posted: Thu May 15, 2014 11:36 am
by Tinka
Hi

Like described in this post http://forums.nubuilder.cloud/viewtopic.p ... 53a#p12634, I would like to update two existing databasefields with the current user and current date as soon as any form field is changed. I would like to do this when an existing record is modified and re-saved.
The methods for the new record work, but what would the onsave code be for a filed record?

I tried different Javascripts without success (because I am a newbie).
F.eks. for the date I have this code on the Javascript of the form:

Code: Select all

//-- Change date is current date
{var today = new Date()

  var dd = today.getDate()
  var mm = currentDate.getMonth() + 1
  var yyyy = currentDate.getFullYear()
  var currentDate = mm+'-'+dd+'-'+yyyy};

  
{pri_chngdate.value = currentDate}; 

}
And on the BeforeOpen Tab:

Code: Select all

pri_chngdate.value = currentDate;
Max, could you advise more details for the OnSave code for current user and current date? Thank you.

Tinka

Re: Fill fields On Save

Posted: Thu May 15, 2014 3:25 pm
by Tinka
I worked out how to populate two fields with current date and current user, if one particular form field changes (Event: Onchange), by doing two Java functions and putting them on the form's Javascript.

Current date:

Code: Select all

function GetTodayDate() {
   var tdate = new Date();
   var dd = tdate.getDate(); //yields day
   var MM = tdate.getMonth(); //yields month
var twoDigitMonth = ((tdate.getMonth().length+1) === 1)? (tdate.getMonth()+1) : '0' + (tdate.getMonth()+1);
   var yyyy = tdate.getFullYear(); //yields year
   var currdate = dd + "-" + twoDigitMonth + "-" + yyyy;

   return currdate;
}
Current user:

Code: Select all

function GetUsername () {
var user = '#nu_user_name#';
return user;
}
But how can I populate these fields if ANY of the form field changes?

Re: Fill fields On Save

Posted: Mon May 19, 2014 12:18 am
by massiws
Tinka, you can check the nuFORM.edited property in nuOnSave():

Code: Select all

function nuOnSave() {
    if (nuFORM.edited) {
        $( "#field_id-for-today-date" ).val( GetTodayDate() );
        $( "#field_id-for-username" ).val( GetUsername );
    }
    return true;
}
Hope this helps,
Max

Re: Fill fields On Save

Posted: Mon May 19, 2014 9:51 am
by Tinka
Thank you, exactly what I was looking for. And working, too :)

BR, Tinka

Re: Fill fields On Save

Posted: Mon May 19, 2014 10:32 pm
by massiws
.