This topic is locked

Guide 62 - SNIPPET refresh by Ajax

5/16/2023 2:33:17 PM
PHPRunner Tips and Tricks
fhumanes author

A few months ago I wrote an article to indicate how the Dashboard Snippets could be refreshed .
In this case, I am going to explain the update of the snippets by Ajax in the rest of the pages, because PHPRunner has many cases in which it refreshes the information by Ajax and if we have indicated a snippet on that page, it would not be refreshed.
As a general policy when writing applications, everything that we can update by Ajax will be very appreciated by the users of that application, so it is highly recommended to update by Ajax instead of refreshing the entire page.
Objetive
Update a Snippet information in INLINE information update. In this case, PHPRunner does the update by Ajax and only updates the GRID information of the records.
DEMO: https://fhumanes.com/snippet_ajax/
If you are interested in this topic, keep reading the article at this link.

fhumanes author 5/16/2023

Technical Solution
Example of the aspect of the example that I have made.
img alt
So that the example is very simple and the solution can be better understood, what I have done is create a Snippet above the buttons that shows the execution date of the Snippet and the highest value of all the records, from the field "Text1" .
The registrations and editions are made INLINE, so the system does not reload the page and therefore the Snippet is not updated. This is what we are going to solve with the following code.
img alt
We are going to need the name of the Snippet that appears in the HTML that PHPRunner generates. You have to follow what the figure indicates.
We are going to create 2 files.
1.- The “ snippet_proj_1.php ” is the snippet code (proper):
`<?php
$rs = DB::Query("select count(*) count from snippet1");
$count = $rs->value("count");
$rs = DB::Query("select max(text1) major from snippet1");
$major = $rs->value("major");

$date = new DateTime();
$date1 = $date->format('Y-m-d H:i:s');
$html = "At the moment, at $date1 we have $count records and the major text is: $major";
echo $html;2.- the " snippet_proj_1 ajax .php ", which is the one that provides the execution context of PHPRunner when the refresh is requested. This can have this same code in all cases. <?php
require_once("../include/dbcommon.php"); // DataBase PHPRunner

require("snippet_proj_1.php");To request the refreshment I have written JavaScript code in the event "JavaScript OnLoad event". this.on('afterInlineAdd', function( fieldsData ) {
// console.log("incio del reload");
$.ajax({ // Reload the Snippet
url: 'MyCode/snippet_proj_1_ajax.php',
dataType: "html",
type: "Get",
data: { }
}).done(function (response) {
// console.log("Justo antes de reload de snippet");
$('span[data-itemid="snippet_proj_1"').html(response);
});
});`On line 10 is the name of the Snippet, to search for it by JQUERY and replace its content with the one received from the URL on line 4.
I think it has been very simple and that you will be able to use it in many circumstances.
For any questions or what you need, contact me through my email: fernandohumanes@gmail.com
I leave you the sources of the project so that you can download and customize it on your PC.