V
V
Vitaly Sorokin2017-03-12 15:39:55
PHP
Vitaly Sorokin, 2017-03-12 15:39:55

How to extract the calculation from JavaScript?

Hello.
Let's assume a hypothetical situation:
There is a product order form. The cost of this product depends on the entered data and is calculated according to a certain formula. On the form, when entering data, the total cost is immediately displayed. But after submitting the form to the server, we need to calculate the cost again using the same formula. This leads us to the fact that the formula must be written in both the JS script and the client script. However, this is very inconvenient for support.
Are there any solutions that allow you to write the formula in one place in the application?
I thought about AJAX or WebSocket, but such a solution loads the server and has low responsiveness.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
X
xutesayor, 2017-03-12
@xutesayor

It's normal practice to duplicate server logic on the client for speed. Php and js are very similar languages, and if you have a pure function without side effects, then it will not be difficult to port it to php.

A
Arman, 2017-03-12
@Arik

isomorphic application code -NodeJS?
and so on the server side, in any case, you need to count again, and if there is no JS, then only support two codes. You can also use Ajaxes, just request the calculation not from each fart, but with some delay.

N
Ninja Mate, 2017-03-12
@victorzadorozhnyy

make a calculation request on the server side by rest api

G
Grigory Vasilkov, 2017-03-12
@gzhegow

Yes, your calculation is performed by a model in PHP, and from Javascript you make a request to this calculation with Ajax.
The main thing is that your Ajax does not cause the loading of the entire site, but simply makes a calculation, then everything will be very fast. How can you understand this - make a request to a non-existent URL - if the site is not hacked - you will get the answer "the server is not responding" or the standard .htaccess Forbidden, and if it is hacked, it will generate a page like "there is no such page".
For example, compare and calculate 2000 products by requesting saving the results in Google Docs along the way, and getting directories from the same Google Docs, and issuing all this as a json array, saving caches - it takes a little more than 10 seconds, and this is without using quick functions - just what was written.
If you do not load the entire framework to call the "calculate" action, but work with the existing database from the cache of goods at prices, the calculation will be done instantly and there will be no sore "loader" for 10 seconds of calculating three goods from the basket. However, an exact request by product id to the database will work just as quickly if it is written with the necessary joins, and not "request-request-request-combine in PHP-calculate-combine-query-check".
However, it is possible and really on some node. The main thing to remember is that when the server does the calculation, it is in operation, that is, the load, when the client does the calculation, it can send you different crap (hackers, yes, yes), which you can’t check without making the calculation on the server.

S
Stalker_RED, 2017-03-12
@Stalker_RED

If the formulas are not very complex, then you can rewrite them so that the code in js and php is completely the same.
For example:

function calc($a, $b, $c, $result = 0) {
   $result = $a + $b;
   $result = $result * $c;
   return $result;
}

This will work the same in both js and php.
Such formulas can be kept in one place and included in both php and js.
Of course, if you need more complex calculations than simple arithmetic, such as sin() calls in php, and in js this is already Math.sin(), then you will have to write additional wrappers.

V
Vitaly Sorokin, 2017-03-12
@SorokinWS

Apparently, there are no ready-made solutions - otherwise there would be an answer right away.
I see 2 possible solutions
1. Request via AJAX or WebSocket application - there are downsides, but support is easier. And with proper optimization, it works fine.
2. generation of duplicate code in automatic mode. That is, the formula can be described in the config, and simply used when calculating on the server. And JS files are rebuilt after changes are made.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question