This topic is locked
[SOLVED]

 Javascript to create and electronic signature

11/11/2018 9:36:44 PM
PHPRunner General questions
woodey2002 author

Hi Guys,
Anyone got any ideas how I can adapt the following Javascript onload event to create and electronic signature.
On my add page when a user clicks the "Staff Sign" checkbox, I would like a the "Staff Signature" field to be automatically updated with the current logged in users"username"along with a timestamp, therefore forming an electronic signature.
The code below allows me to insert some text when the checkbox is ticked but I'm struggling to pull the username and timestamp.
I'm, trying to use something like $_SESSION["username"] and now() with no luck.
Any ideas would be greatly appreciated.
Thanks

J



var ctrlIvc50 = Runner.getControl(pageid, 'staff_Sign');

var ctrlIvc50T = Runner.getControl(pageid, 'staff_Signature');
ctrlIvc50.on('change', function(e) {
if (this.getValue()=='on'){
ctrlIvc50T.setValue("Expires Soon");

ctrlIvc50T.addValidation("IsRequired");

}
else {
pageObj.hideField("staff_Signature");

ctrlIvc50T.removeValidation("IsRequired");

ctrlIvc50T.setValue('');



}

});
admin 11/12/2018

$_SESSION["username"] and now() are server side functions and won't work in Javascript. You have two options here.

  1. Pass $_SESSION["username"] and now() to Javascript using proxy method.
  2. Or use events like BeforeAdd or BeforeEdit where you can examine the value of staff_Sign field and populate staff_Signature field if required.

woodey2002 author 11/13/2018

Many thanks Sergey.
I see what you are saying.
I have passed the username via a proxy as suggested.
I can now successfully autofill the current username and a timestap when a user clicks the checkbox.
Staff Signature

admin Wed Nov 14 2018 03:27:17 GMT+0000 (Greenwich Mean Time)
My only problem now is how to format the return of the timestamp.
As can be seen above, I get the username as desired but for the date field I get a extra long field including the timezone e.g. Wed Nov 14 2018 03:27:17 GMT+0000 (Greenwich Mean Time)
I really only need dd/mm/yyyy hh:mm so has anyone got any ideas how I can format the returned date with no timezone?
Many thanks

J

var ctrlIvc50 = Runner.getControl(pageid, 'staff_Sign');

var ctrlIvc50T = Runner.getControl(pageid, 'staff_Signature');

var d = Date ();

A= d.toString ()
var res = (proxy['un']).concat (" ", A);
ctrlIvc50.on('change', function(e) {
if (this.getValue()=='on'){
ctrlIvc50T.setValue(res);

ctrlIvc50T.addValidation("IsRequired");


}
else {


ctrlIvc50T.removeValidation("IsRequired");

ctrlIvc50T.setValue('');



}

});
admin 11/14/2018

Before you pass the value of now() to Javascript use PHP's date function to format it. See examples at http://php.net/manual/en/function.date.php

woodey2002 author 11/14/2018

Thanks Sergey,
For the benefit of our community here is the final solution.
Add this code to the Add page before display event to get the current logged in username
$pageObject->setProxyValue("username", $_SESSION["UserID"]);
Add this code to the Add page Javascript OnLoad event to insert current logged in username along with date when a user clicks a checkbox therefore creating a digital signature.



//Start.............................................................................

var ctrlIvc50 = Runner.getControl(pageid, 'staff_Sign');

var ctrlIvc50T = Runner.getControl(pageid, 'staff_Signature');
var today2 = new Date();

var dd = today2.getDate();

var mm = today2.getMonth() + 1; //January is 0!
var yyyy = today2.getFullYear();

if (dd < 10) {

dd = '0' + dd;

}

if (mm < 10) {

mm = '0' + mm;

}

var today2 = dd + '/' + mm + '/' + yyyy;
var res = (proxy['username']).concat (" ", today2);
ctrlIvc50.on('change', function(e) {
if (this.getValue()=='on'){
ctrlIvc50T.setValue(res);

ctrlIvc50T.addValidation("IsRequired");


}
else {


ctrlIvc50T.removeValidation("IsRequired");

ctrlIvc50T.setValue('');



}

})
;

//End
A
ayctech 12/6/2018

can you assign a value to the session variable from javascript?