This topic is locked
[SOLVED]

 Date Differences in PHPRunner

7/3/2016 2:13:34 PM
PHPRunner General questions
A
admin author

Hi, I am fairly new to PHP Runner and advanced coding.
I have 4 core fields for this requirement

start (Date Field)

end (Date Field)

datediff (Text Field)

2 years (Checkbox)
The requirement is as follows on the Add page:
End date loses focus or is changed -> populate datediff field with "X" (total months)

If datediff > 24 -> set 2 years = "true"

If 2 years = False -> show hidden fields
Any help on how to correctly code this would be greatly appreciated. I have been reading the manuals and a lot of google searching for 2 days and nothing I have tried will work correctly.

C
copper21 7/3/2016

Although I can't get you exactly what you need, you will have to use javascript for this. Type "javascript date" in the forum search and this should get you started.

A
admin author 7/3/2016



Although I can't get you exactly what you need, you will have to use javascript for this. Type "javascript date" in the forum search and this should get you started.


Thank you, I have searched through the forums as well as web searches. I am fairly familiar with JavaScript. I am focusing on the date diff at the moment.
I added two fields

strtotime_start

strtotime_end
Placed the code below, just to populate the strtotime when start loses focus. I even tried it with change and nothing happens.

var ctrlstart = Runner.getControl(pageid, 'start');

var ctrlstrtotime = Runner.getControl(pageid, 'strtotime_start');
ctrlstart.on('blur',function() {

// call 'setValue' method to set new value

ctrlstrtotime.setValue(strtotime,(ctrlstart.getValue()));
});


I even tried ctrlstrtotime.setValue(Date,(ctrlstart.getValue()));and in the View As $value = strtotime("start").
I can get the checkbox and hidden fields working. It's the .on and setValue I am having the struggles with.
Thanks!

Admin 7/5/2016

While I don't have an answer to this specific question there are troubleshooting steps you need to take:

  1. Make sure there are no Javascript errors thrown
  2. Make sure your code in 'on' event is execute. You can do that adding function like alert() there.
  3. Once you figured out that code is in fact is executed you need to troubleshoot it a little further. Set a breakpoint inside 'on' function handler and inspect variable values to see what is going on.
    This article can help:

    http://xlinesoft.com/phprunner/docs/troubleshooting_javascript_errors.htm

A
admin author 7/5/2016



While I don't have an answer to this specific question there are troubleshooting steps you need to take:

  1. Make sure there are no Javascript errors thrown
  2. Make sure your code in 'on' event is execute. You can do that adding function like alert() there.
  3. Once you figured out that code is in fact is executed you need to troubleshoot it a little further. Set a breakpoint inside 'on' function handler and inspect variable values to see what is going on.
    This article can help:

    http://xlinesoft.com/phprunner/docs/troubleshooting_javascript_errors.htm


Thank you for the recommendation.

A
admin author 7/6/2016

Thanks for the suggestions and links. Although helpful, they appear to me to over complicate things a bit.
I able to the following with fairly simple code. I am sharing the code below in case other newbies to PHPRunner run across similar challenges.

  • DATE_DIFF - without the need of addition strtotime translation
  • Select/Unselect Checkbox
  • hide/show fields with the simple code pasted below:

  1. Set up 2 proxy values on the Before Display
  2. Add code on Javascript onload event
    if (proxy.start='""'){

    pageObj.hideField('strtotime_start');

    var ctrlstart = Runner.getControl(pageid, 'start');

    var ctrlstrt = Runner.getControl(pageid, 'strtotime_start');

    var check = Runner.getControl(pageid, 'checkbox');

    ctrlstart.on('blur', function() {

    // call 'setValue' method to set new value

    if(ctrlstart.getValue() != "" && ctrlend.getValue() != ""){

    var date1 = new Date(ctrlstart.getValue());

    var date2 = new Date(ctrlend.getValue());

    var numMonths = (date2.getFullYear() - date1.getFullYear()) 12 + (date2.getMonth() - date1.getMonth());

    if(date1.getDate() > date2.getDate())

    numMonths--;

    alert(numMonths + "");

    if(numMonths < 24)

    {

    check.setValue(true);

    pageObj.showField('strtotime_start');

    pageObj.showField('strtotime_end');

    }

    else

    {

    check.setValue(false);

    pageObj.hideField('strtotime_start');

    pageObj.hideField('strtotime_end');

    }

    ctrlstrt.setValue(ctrlstart.getValue()+"");

    }

    });
    if (proxy.end='""'){

    pageObj.hideField('strtotime_end');

    var ctrlend = Runner.getControl(pageid, 'end');

    var ctrlstrtend = Runner.getControl(pageid, 'strtotime_end');

    var check = Runner.getControl(pageid, 'checkbox');

    ctrlend.on('blur', function() {

    // call 'setValue' method to set new value

    if(ctrlstart.getValue() != "" && ctrlend.getValue() != ""){

    var date1 = new Date(ctrlstart.getValue());

    var date2 = new Date(ctrlend.getValue());

    var numMonths = (date2.getFullYear() - date1.getFullYear())
    12 + (date2.getMonth() - date1.getMonth());

    if(date1.getDate() > date2.getDate())

    numMonths--;

    alert(numMonths + "");

    if(numMonths < 24)

    {

    check.setValue(true);

    pageObj.showField('strtotime_end');

    pageObj.showField('strtotime_start');

    }

    else

    {

    check.setValue(false);

    pageObj.hideField('strtotime_end');

    pageObj.hideField('strtotime_start');

    }

    ctrlstrtend.setValue(ctrlend.getValue()+"");

    }

    });

    } }
    Hope this helps someone!