How to assign today date of Client PC to a field?

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
YogiYang
User
Posts: 101

How to assign today date of Client PC to a field?

Post by YogiYang »

Hello,

I am trying to assign Today's date to a date field in form as load. The date has to come from the users PC and not from server.

I tried to assign ew_currentdate but then the field disappears from the form.

So how can we assign current user's PC date at load time?

TIA

Yogi Yang


arbei
User
Posts: 9292

Post by arbei »

To add the date-time value with the client pc, you need to write your code in Startup Script and assign it with the jQuery val() function.

For example:
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
$("<Selector>").val(date);


mobhar
User
Posts: 11660

Post by mobhar »

Assume you want to load the today date to "MyDate" field, simply put the following code in "Row_Rendered" server event:

if (CurrentPageID()=="add") {
$this->MyDate->EditValue = ew_CurrentDateTime();
}


YogiYang
User
Posts: 101

Post by YogiYang »

mobhar wrote:
Assume you want to load the today date to "MyDate" field, simply put the
following code in "Row_Rendered" server event:

if (CurrentPageID()=="add") {
$this->MyDate->EditValue = ew_CurrentDateTime();
}
Will this return server's date or client's PC date?


mobhar
User
Posts: 11660

Post by mobhar »

It will return server's date.


YogiYang
User
Posts: 101

Post by YogiYang »

Oh! I see.

What I want is clients date.

Any idea as to how to implement this?

TIA

Yogi Yang


Chris
User
Posts: 162

Post by Chris »

Client time is represented easily with moment(), bundled in PHPMaker.

var client_time = moment().format();
$("#client_time").val(client_time);


YogiYang
User
Posts: 101

Post by YogiYang »

Chris,

Thanks for the tip.

I tried the code in Client Script -> Table Specific -> Add/Copy Page -> Client Script and also Startup Script but nothng turned up so I placed it at Field's Client size event. And it works!!
Here is the complete working script:
"focus": function(e) {
// Your code
var client_time = moment().format("DD/MM/YYYY");
var $row = $(this).fields();
//alert(client_time);
$row["cb_book_date"].value(client_time);

}

Regards,

Yogi Yang


YogiYang
User
Posts: 101

Post by YogiYang »

Is there any way by which we can detect as to whether the script is running in add/copy page or edit page?


Chris
User
Posts: 162

Post by Chris »

It will run on your page as long as you put the script into the table-specific Add/Copy page after the page loads, i.e. with $(document).ready, so add this:

$(document).ready(function(e) {
if (EW_PAGE_ID == 'add') {
var client_time = moment().format("DD/MM/YYYY"); // uses the format of locale if empty
$("#x_cb_book_date").val(client_time);
}
});

Note that value() is not a jQuery function, use val(). It's not necessary to use the fields() plugin for this purpose, as it will load and then store all the form data on the entire page in an array in memory. Instead, access the specific field by the element's ID, usually the field name prefixed by x_, so in your case I'd look at the page source and verify you have an field with the id "x_cb_book_date".

You can tell the type of page by checking the constant EW_PAGE_ID (shown above). It's also found in your page source.


Post Reply