JavaScript logic displacement to database – Xlinesoft Blog


In this article we will talk about how to minimize the amount of code that JavaScript manages on the added/edit pages. Making your data -based code will help you easily manage forms with a large number of fields. We will take care of functionality such as showing/hiding fields, making reading fields, mandatory, disabled, etc.

This approach will involve the following steps
1. Creation and popular database tables to store triggers (when logic is applied) and actions (what happens when shutting down)
2. PHP Code and C# of the server passing this data to JavaScript
3. The JavaScript code that listens to the “change” event and implement the logic defined in the database.


Database tables

Table triggering. The structure is quite simple. You can see that we store the name of the table, the type of page, the name of the field and which event we hear. In this article we will only show how to implement the most common “change” event, as it covers 95% of the required functionality.

The actions table is a detail, while it triggers it is a teacher. They are linked by the Trigger_id field. This table knows what condition to be checked and what action to be performed. We look at the front row and try to decipher it.

The action is linked to the triggering #1 (change of country event event on the client edit page). The condition is the same and the condition_Value is “USA”. It means that when the country’s field is equal to “USA”, we should continue with our action. And the action itself showing the region’s field.

And here is the SQL script that will create the two tables with sample data.

Server code

The server code goes to BEFOREDISPlay event of each page where this functionality must be implemented. In our situation, this code goes to the editing page of the BEFOREDISPLAY CUSTOMER TABLE. The code itself is quite simple. It only sends all the data of the shares and triggers the tables and makes this data available to JavaScript by means of proxy.

Please note that for a better code structure you must create a PHP or C# external file, define a function there and put this code in this feature. Then in all Beforedisplay events

PHP code

Code C#

JavaScript Code

The JavaScript code is directed to the JavaScript load event on the page where the action should take, in our case, this is the editing page of the customer JavaScript table. The same idea with creating a JavaScript function and calling from the external file is also valid here.

You can expand this code by adding more types of conditions and more actions. Check the comments online for more information.

Enjoy!



Technology

Berita Olahraga

Lowongan Kerja

Berita Terkini

Berita Terbaru

Berita Teknologi

Seputar Teknologi

Berita Politik

Resep Masakan

Pendidikan

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

Dark theme admin panel
Learn how to create an amazing dark themed admin dashboard with interactive charts and statistics…