Add alert message linking date with reference number?

Got something cool to share with AppGini users? Feel free to post it here!
Post Reply
User avatar
lramirez
Veteran Member
Posts:114
Joined: 2019-11-01 23:23

Add alert message linking date with reference number?

Post by lramirez » 2025-11-11 00:45

Hello forum members. ;)
I'd like to ask for help with a project. I have a database where I capture information on the same day, with a specific number "12345,"
t1_a.png
t1_a.png (24.53 KiB) Viewed 3677 times
but then minutes or hours later I capture the same information again with the same number "12345." I want the application to notify me when this happens, but only with a message before saving.
t2_b.png
t2_b.png (27.89 KiB) Viewed 3677 times
Summary: I want the application to notify me: for example: "The same information is being captured today"

It could be a message similar to the one used when "unique" is entered in a field that cannot be repeated, or "required" when the field cannot be left empty—that type of alert message.

Is this possible?

Thank you. :)
Luis Ramirez R.

ppfoong
Veteran Member
Posts:94
Joined: 2021-07-13 16:46

Re: Add alert message linking date with reference number?

Post by ppfoong » 2025-11-11 03:32

Yes, you can make use of the "before_insert" function in your hook file.

Check your hooks folder for the php file with the same name of the table. Open it.

Search for the function <tablename>_before_insert

Execute a Select SQL statement to do your checking, if record found, prompt the message and stop the insert.

User avatar
lramirez
Veteran Member
Posts:114
Joined: 2019-11-01 23:23

Re: Add alert message linking date with reference number?

Post by lramirez » 2025-11-11 17:47

Okay, thanks.
(Sorry my project is in Spanish)
I tried this...
:|

Code: Select all

function form_visita_before_insert(&$data, $memberInfo, &$args){
	$visitante = $data['number'];
	$fecha = $data['date'];

	$sql = "SELECT COUNT(*) AS total 
			FROM form_visita 
			WHERE number = '{$visitante}' 
			AND date = '{$fecha}'";
	$res = sql($sql, $eo);
	$row = db_fetch_assoc($res);

	if($row['total'] > 0){
		echo "<script>
			Swal.fire({
				icon: 'info',
				title: 'Message',
				text: 'This visitor number was already captured today, but the new record can be saved.',
				confirmButtonText: 'Ok'
			});
		</script>";
	}

	return TRUE; 
}
and I think it works... I'm confused about sending the message... to the screen, preferably to template-DV or if not, to template-TV.

The only thing that appears is the default AppGini message: new record saved

Please. Thank´s :|
Luis Ramirez R.

saymaad
AppGini Super Hero
AppGini Super Hero
Posts:131
Joined: 2024-06-03 16:17

Re: Add alert message linking date with reference number?

Post by saymaad » 2025-11-12 08:48

Echoing <script> in a backend hook (before_insert) is unreliable. It may not render properly, especially if the insert is triggered via AJAX or API. Instead try the following:

Code: Select all

function form_visita_before_insert(&$data, $memberInfo, &$args) {
    $visitante = makeSafe($data['number']);
    $fecha = makeSafe($data['date']);

    $exists = sqlValue("SELECT COUNT(*) FROM form_visita WHERE number='{$visitante}' AND date='{$fecha}'");

    if($exists > 0) {
        WindowMessages::addDismissable(
            "This visitor number was already captured today. Please verify before proceeding.",
            "alert alert-warning"
        );
        // Optionally block the insert by uncommenting the following line, this will make sure no duplicate numbers are saved:
        // return false;
    }

    return true;
}
Optional Client-Side Validation
In AppGini 24.18 and above {tablename}_customValidateData will automatically be called during detail view form submission and if it returns false, the form will not be submitted. You can define a custom validation function in any of the following locations:
  • hooks/header-extras.php
  • hooks/footer-extras.php
  • {tablename}-dv.js
  • {tablename}_dv() hook function in hooks/{tablename}.php

Code: Select all

function form_visita_customValidateData(insertMode) {
    const visitante = $j('#number').val();
    const fecha = $j('#date').val();
    let result = false;

    $j.ajax({
        url: 'hooks/check_duplicate_visit.php',
        type: 'POST',
        data: { number: visitante, date: fecha },
        async: false,
        success: function(response) {
            if(response.exists) {
                AppGini.modalError('This visitor number was already captured today.');
                result = false;
            } else {
                result = true;
            }
        }
    });

    return result;
}
hooks/check_duplicate_visit.php

Code: Select all

<?php
include(dirname(__FILE__) . '/../lib.php');

$number = makeSafe($_POST['number']);
$date = makeSafe($_POST['date']);

$exists = sqlValue("SELECT COUNT(*) FROM form_visita WHERE number='{$number}' AND date='{$date}'");

echo json_encode(['exists' => ($exists > 0)]);

User avatar
lramirez
Veteran Member
Posts:114
Joined: 2019-11-01 23:23

Re: Add alert message linking date with reference number?

Post by lramirez » 2025-11-13 03:37

Excellent, thank you very much. I made a few minor adjustments, and everything works perfectly. Thanks!

Code: Select all

	function form_visita_before_update(&$data, $memberInfo, &$args) {
		$visitante = makeSafe($data['visitante']);
		$fecha = makeSafe($data['fecha']);

		$exists = sqlValue("SELECT COUNT(*) FROM form_visita WHERE visitante='{$visitante}' AND fecha='{$fecha}'");

		//Get visitor name and ID number from the visitor_list table
			$visitanteData = sql("SELECT CONCAT(nombres, ' - ', idcard) AS visitante_info FROM lista_visitante WHERE id = '{$visitante}'", $eo);

			$row = db_fetch_assoc($visitanteData);
			$nombreIdCard = $row['visitante_info'];

		if($exists > 0) {
		$fechaFormateada = date("d/m/Y", strtotime($fecha));	
		WindowMessages::addDismissable(
				"The visit:<b> {$nombreIdCard}</b>, was already captured today <b>{$fechaFormateada}</b>.
				 == PLEASE VERIFY THE INFORMATION BEFORE PROCEEDING WITH A NEW REGISTRATION ==.",
				"alert alert-warning"
			);
		}

		return TRUE;
	}

Captura de pantalla 2025-11-12.png
Captura de pantalla 2025-11-12.png (27.64 KiB) Viewed 3592 times
THANK YOU SO MUCH... ;)
Luis Ramirez R.

Post Reply