Display popup after insert a new record in a table

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
balfons
Veteran Member
Posts: 91
Joined: 2018-10-22 15:27

Display popup after insert a new record in a table

Post by balfons » 2022-07-25 08:58

Hi!

I'm triying to display a popup after insert a new record in a table. I have searched in many forums and this is my code:

Code: Select all

function function_alert($message) {// I define the function to display the alert box
     
	echo "<script>alert('$message');</script>";
}

//Then, I call the function in after insert of the php file. In this case is regulations_systems.php

function regulations_systems_after_insert($data, $memberInfo, &$args) {

		function_alert("Hi! You added a new item. Please, check it up!");
		insert_nucli($data); //This is another function I need to call after insert a new record. This one works fine

		return TRUE;
}
But, nothing happens and no popup is displayed. Any ideas of what is wrong in my code? Any othen suggestion for doing this?

Thanks in advance!

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Display popup after insert a new record in a table

Post by jsetzer » 2022-07-25 11:55

Possible Reason

Well, just see the sequence of function calls when inserting a record:
  • UI: You click #insert button
  • Browser POSTs data to server
  • AppGini code executes before_insert()-hook
  • AppGini code inserts the record into the database
  • AppGini code calls after_insert()-hook
    (here you echo "<script>alert('$message');</script>";
  • AppGini reloads the Detail View (or redirects to a different page, if configured)

Due to the immediate reload of Detail View (showing inserted record) your echo-command in after_insert()-hook does not have any effect on the newly loaded Detail View page, I guess.

Possible Solution

So, your task is to pass your $message from after_insert()-hook into the Detail View and alert it once (=not on every further load of detail view).

What about the following:
  1. In TABLENAME_after_insert()-hook populate a $_SESSION-variable with your message
  2. In TABLENALE_dv()-hook, if $selectedID exists, check if that $_SESSION-variable is defined.
  3. If it is, append your <script>...</script> to $html variable...
  4. and unset the $_SESSION variable (to avoid another alert on next DV-opening)
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Display popup after insert a new record in a table

Post by jsetzer » 2022-07-25 12:03

Starting point for your own implementation, this is working for me:

Code: Select all

function TABLENAME_after_insert($data, $memberInfo, &$args)
{
	$message = "I will be displayed once after insert";
	$_SESSION["DISPLAY_MESSAGE"] = $message;
	return TRUE;
}

Code: Select all

function TABLENAME_dv($selectedID, $memberInfo, &$html, &$args)
{
	if ($selectedID && isset($_SESSION["DISPLAY_MESSAGE"])) {
		$html .= '<script>alert("' . $_SESSION["DISPLAY_MESSAGE"] . '");</script>';
		unset($_SESSION["DISPLAY_MESSAGE"]);
	}
}
Result

6e4F6eSRTm.png
6e4F6eSRTm.png (5.56 KiB) Viewed 1390 times
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Display popup after insert a new record in a table

Post by jsetzer » 2022-07-25 12:18

Tipp

If your browser supports Notification API, check out this following Notification instead of the blocking browser Alert:

Code: Select all

$html .= '<script>new window.Notification("Inserted", {body: "'.htmlentities($_SESSION["DISPLAY_MESSAGE"]).'"});</script>';
Attachments
notification-api.png
notification-api.png (93.07 KiB) Viewed 1389 times
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

balfons
Veteran Member
Posts: 91
Joined: 2018-10-22 15:27

Re: Display popup after insert a new record in a table

Post by balfons » 2022-07-26 10:36

Hi Jan!

thanks for your quick answer! I'll implement what you suggest and let you know if it works.

Regards

balfons
Veteran Member
Posts: 91
Joined: 2018-10-22 15:27

Re: Display popup after insert a new record in a table

Post by balfons » 2022-07-26 12:18

Hi again, Jan!

I have just implemented the code you suggested in TABLENAME_after_insert and TABLENAME_dv and it works! It will be very useful for our project.

Thanks again

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Display popup after insert a new record in a table

Post by jsetzer » 2022-07-26 16:56

Thanks for your feedback.
Did you try with the Browser-Notifications? Does it work for you?
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

Post Reply