PHP JavaScript deferred Loading: Difference between revisions

From HaFrWiki42
Jump to navigation Jump to search
Line 85: Line 85:
This PHP file is used in the Frontend and in the Backend.  
This PHP file is used in the Frontend and in the Backend.  
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
// The Constant definitions for the creation of the form (PHP) and the AJAX Client call (JavaScript) and the Mapping of the form Variables to the Backend (PHP). <ref name="AH-1">AdminHomeDb.php - Constant declaration for the deferred Form.</ref>
// [1] - The Constant definitions for the creation of the form (PHP) and the AJAX Client call (JavaScript) and the Mapping of the form Variables to the Backend (PHP).
const HS_HOME_ID            = "hsHomeId";
const HS_HOME_ID            = "hsHomeId";
const HS_HOME_NAME          = "hsHomeName";
const HS_HOME_NAME          = "hsHomeName";
</syntaxhighlight>
</syntaxhighlight>
* [1] Constant declaration <ref name="AH-1">AdminHomeDb.php - Constant declaration for the deferred Form.</ref>


== See also ==
== See also ==

Revision as of 16:47, 30 May 2018

Deferred loading is a programming technique to improve the performance of Websites by loading code only when needed. The purpose of this article is to show how to use deferred loading of Webpage parts using PHP and JavaScript in combination with AJAX and jQuery.

Deferred loading can also be used on CSS, but that is not the scope of this article.

Introduction

When a website loads, not all content is always necessary. So why should you load such content if you don't know the use is gonna use it? In this article is assumed that the website consist on (Bootstrap) navigation tabs. The user can have several roles that gives or denies him access to certain parts of the website. If the user has the admin-role, he may access everything including the admin tab. In that case you don't want to load the admin tab at all if the user hasn't the admin-role. But also if the user has the admin-role you do need the admin tab every time you access the website.

In that case you wanna use deferred loading.

Requirements

The framework we are going to make depends on PHP-server-coding and JavaScript-Client-coding. To clue the 2 parts we are using AJAX in combination with jQuery. The application loads a form with JavaScript validation and AJAX calls for checking and CRUD operations on the Database. The form has a Bootgrid part for showing the content of the database.

Used files

FileName Description
AdminHomeDb.php Front- and Backend for constants (variable-names), Form creation, Database CRUD methods and Database CRUD checks.
AppValidateJS.php Frontend, generic validation and patterns, Bootgrid variables, Event for validation (on-blur).
AdminHomeJS.php Frontend, form specific validation and patterns, validation objects, Events for specific validation (on-blur), AJAX getters for the form, AJAX CRUD, Timed Events for the form.
App.php Main application PHP page, which contains the JavaScript .ready method.
App-co.php Common PHP file which contains the link between the PHP-Frontend and the PHP-Backend, such as the Database connection and the common include files.

For this framework it must contain the AJAX actions and request definitions.

App-be.php Backend PHP file which contains the AJAX-Backend implementations.

Specific for the PHP deferred loading this file contains the Form, Form CRUD and Form CRUD Checks Backend implementations.

  • The App in the FileName is the MainApp, meaning the Main PHP Page. The App is split into 3 files App.php, App-co.php and App-be.php.
  • The AdminHome in the FileName is the WebPage Tab (deferred Form).

Form Validation

The Application validation is based on Regex with validation objects (validator).

Validation Object

A validator for a string-name has the following JS-object [1]. <syntaxhighlight lang="javascript"> var validHomeName = {

  environ  : "Home",
  name     : "<?php echo HS_HOME_NAME; ?>",
  qid      : sprintf('[name="%s"]'  , "<?php echo HS_HOME_NAME; ?>"),
  qcb      : sprintf('[name="cb%s"]', "<?php echo HS_HOME_NAME; ?>"),
  pattern  : "^[A-Za-z0-9À-ž&@#$\\*\\+\\'\\<\\>\\.\\,\\;\\:\\-\\_\\(\\)\\[\\]\\/ \\n]{3,20}",
  message  : "Length [3..20], alphanumeric",
  minlen   : 3,
  maxlen   : 20,
  flag     : "gi",
  value    : "",
  status   : jsoStatus

}; </syntaxhighlight>

Field element Field description
environ The environment for the form. In this case it is all about a house/home.
name The name (label of the html-element). The PHP-Label-Definition is made in the AdminHomeDb.php OUTSIDE the class definition.


AdminHomeDb.php

This PHP file is used in the Frontend and in the Backend. <syntaxhighlight lang="php"> // [1] - The Constant definitions for the creation of the form (PHP) and the AJAX Client call (JavaScript) and the Mapping of the form Variables to the Backend (PHP). const HS_HOME_ID = "hsHomeId"; const HS_HOME_NAME = "hsHomeName"; </syntaxhighlight>

  • [1] Constant declaration [2]

See also

top

Reference

top

  1. JS = JavaScript
  2. AdminHomeDb.php - Constant declaration for the deferred Form.