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!