This topic is locked

Custom Button on Edit Form to Set Current DateTime in end_time Field Not Updating Record

9/28/2024 5:53:29 PM
PHPRunner General questions
N
Nicolas Yacumo author

Hello PHPRunner community,

I'm currently working on a project where I need to add a custom button in the edit form that, when clicked, will set the current date and time into a field (end_time) and update the record in the database.

What We're Trying to Achieve:
The goal is to:

Create a custom button on the edit form that, when clicked, inserts the current date and time into the end_time field (in the format YYYY-MM-DD HH:MM:SS).
After the button is clicked, the list page should be updated, and the new date/time should be saved in the database.
What We Have Done So Far:
Client-Side (Client Before Event): I have written JavaScript code to capture the current date and time in the correct format (YYYY-MM-DD HH:MM:SS) when the button is clicked and set the value for the end_time field:

function OnBefore(pageObj, params, ajax, ctrl, row) {
var now = new Date();
var year = now.getFullYear();
var month = ('0' + (now.getMonth() + 1)).slice(-2);
var day = ('0' + now.getDate()).slice(-2);
var hours = ('0' + now.getHours()).slice(-2);
var minutes = ('0' + now.getMinutes()).slice(-2);
var seconds = ('0' + now.getSeconds()).slice(-2);

var formattedDateTime = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;

params["end_time"] = formattedDateTime;
ajax.setMessage("Setting current date and time...");

}
Server-Side Event: i am passing the end_time value from the client to the server-side and then updating the record in the trips table with the new end_time value:

global $conn;

$record = $button->getCurrentRecord();
$end_time = $params["end_time"];

// Prepare the SQL query to update the end_time field
$strSQLUpdate = "
UPDATE trips
SET end_time = '" . db_addslashes($end_time) . "'
WHERE id = " . $record["id"] . ";";

// Execute the update query
db_exec($strSQLUpdate, $conn);
The Problem:
Despite the above setup, when I click the custom button, the form seems to process the request, but the end_time field is not updating in the database nor in the list page. I've tried debugging by checking the SQL query being sent and it looks correct, but the end_time field remains unchanged after submitting. updating the field directly on the db works too.

Additional Notes:
I am working on the edit form, and the end_time field is a datetime field.
I have confirmed that the date picker (if used manually) updates the end_time field correctly, so the field and database are configured properly.
The custom button works for other types of updates but seems to fail specifically for updating the end_time field with the current date and time.
Does anyone have suggestions on what might be going wrong or how to properly configure the button to update the end_time field? I appreciate any help or advice!

Thanks in advance!

N
Nicolas Yacumo author 9/30/2024

No one? i just need a custom button that set current date into a date field....

HJB 10/1/2024

In my opinion, there is NO need of a custom button plus coding at all.

Under EDIT AS, as seen below, an auto-update value for each field can be pre-defined.

img alt

N
Nicolas Yacumo author 10/1/2024

thanks ill try it! the idea is to be able to finish an on going trip, for that i have to place on a button wich will be for usert o end the trip, the current date/time. Thanks for your answer!