Using alertify for all pages

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

Using alertify for all pages

Post by alex »

Hello! I use alertify for certain pages in Page Data Rendering event and now I need to use it for all pages. Here is my code:

// Page Head

AddStylesheet("/alertifyjs/css/themes/bootstrap.css");
AddStylesheet("alertifyjs/css/alertify.css");
AddClientScript("alertifyjs/alertify.min.js");

// Page Data Rendering event

if (something) {
	echo "<script>
	loadjs.ready('head', function() {
	alertify.error('error msg!');
	})
	</script>";
}

I tried to use in in Global > Page Rendering event but I get error "Uncaught ReferenceError: loadjs is not defined"

Please advise how to use alertify for all pages of the project. (v2024)


arbei
User
Posts: 9384

Post by arbei »

  1. You should include your base path of your site like AddStylesheet("/basepath/alertifyjs/...") and AddClientScript("/basepath/alertifyjs/..."). Make sure you put your "alertifyjs" folder under the root folder of the project folder. Replace "basepath" by your real base path.
  2. Page_Rendering is not for outputting HTML. If you use that, you output HTML earlier than everything else so loadjs is not ready yet.
  3. You can use Page_DataRendering but you should add the HTML to the $header argument, not echoing HTML directly.
  4. If you use AddClientScript(), note that the script is also loaded by loadjs, you need to wait until it is loaded before using it, so you better add an id, e.g.
    AddClientScript("/basepath/alertifyjs/alertify.min.js", "alertifyjs");
    loadjs.ready("alertifyjs", () => { ... });

alex
User
Posts: 267

Post by alex »

Thank you but the code is working for separate pages of the project.

But now I need to make working it for all pages of the project. In order not to copy-paste same code to all pages of the project (in Page Data Rendering event).

Is it possible to use a Global code and how?


arbei
User
Posts: 9384

Post by arbei »

alex wrote:

In order not to copy-paste same code to all pages of the project (in Page Data Rendering event).

Why is it necessary? You use alertify only at where you need to use it, e.g. when you have an error to show. Usually the page is already loaded at that time, you may use it directly, you don't need to always use loadjs.ready().

Note that Page_Head is for all pages, you have added alertify to all pages.


alex
User
Posts: 267

Post by alex »

Why is it necessary?

Well, I need to get informed about monthly payments from database (every day of the month):

select id, Day(payDate) from loans where Day(payDate) = Day(NOW())

It can be used for any other reminders lets say users birthdays (from database).

Please let me know if you have better idea.


arbei
User
Posts: 9384

Post by arbei »

You may Create Your Own API Action, call it when you need the information and alert user if necessary. In fact, unless you like alertify, otherwise you may simply use the built-in alert by SweetAlert2, e.g. ew.alert("my message").


alex
User
Posts: 267

Post by alex »

Thank you!


Post Reply