PHP - Javascript

Asked

Viewed 56 times

2

I’m new in the area of web programming and I’m developing a site in php, php itself until I understand well how it works but as for javascript I’m not very sure about when to use.

for example, and a screen of the site there is a table with approximately 100 items that are sent by PHP, I had made a pagination to better view the data on the page, however I did it by PHP itself and for business reasons the decision was made to always load the 100 records instead of loading them per page as I had done in php.

In this case I found it interesting to use javascript to manipulate html and make a dynamic paginated table, but it is worth mentioning that when clicking on these data javascript also fills inputs that can perform an update via PHP in the BD.

Is this kind of approach correct ? PHP -> Javascript -> PHP ... When do I know I should use javascript ?

2 answers

0


Main difference

PHP: Language running on the server.

JS: Language running on the customer.

In your example, if you had 1 million records, you would download all this content to the client’s machine and only then do the treatment.

In these cases, for performance reasons, we should treat the information on the server, not to "jam" the client.

When to use:

This varies widely.

Each case is really a case. JS can make your application more dynamic.

For example, with the use of ajax, we can exchange information on the screen (or even the whole screen) without re-loading the page.

On the other hand, you will not be able to make a JS script in the client to make an SQL query in the database on the server.

Paging:

As a last tip, I advise you to page your information on the server, or even in the database, to improve the performance of your page.

And watch out. JS was born on the client but today, with nodejs, you can use it on the server as well. In this way, always be attentive to the context you are developing.

0

Your previous implementation using PHP was more correct because you don’t need to send 100 items from a table and will only display 10 for example, ends up affecting unnecessarily the performance. So it was correct to send only what will be displayed, frameworks and table components use this way of implementing.

Is this type of approach correct ? PHP -> Javascript -> PHP

Why wouldn’t it be? This is exactly what many front-end tools do (React, Angular, etc...), they use Javascript to do just that, perform requests (via Ajax) for the back-end, in your case PHP, receive the response and display on screen.

When I know I should use javascript ?

You CAN use across your front, as I said before, that’s just what many front-end frameworks do, but you MUST use when you need to do a user interaction, or something dynamic.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.