PHP - Javascript

2

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

For example, and a site screen there is a table with approximately 100 items that are sent by PHP, I had done a pagination to better visualize the data on the page, however I did this by PHP itself and for business reasons was taken the decision 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 paged table, but it is worth mentioning that clicking on this data also fills in inputs that can perform an update via PHP in BD.

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

    
asked by anonymous 29.03.2018 / 22:49

2 answers

0
  

Main difference

PHP: Language that runs on the server.

JS: Language that runs on the client.

In your example, if you had 1 million records, you would download all of that content to the client machine and only then do the processing.

In these cases, for performance reasons, we must treat the information on the server, so as not to "bog down" the client.

  

When to use:

This varies greatly.

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 entire screen) without doing a reload on the page.

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

  

Pagination:

As a final tip, I advise you to page your information on the server, or even 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 aware of the context you are developing.

    
29.03.2018 / 23:05
0

Your previous implementation using PHP was more correct because you do not need to send 100 items from a table and only 10 are displayed, for example, it ends up unnecessarily affecting performance. So it was only right to submit what is going to be displayed, frameworks and table components use this mode to implement.

  

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

Why not? This is exactly what many front-end tools do (React, Angular, etc ...), they use JavaScript to do just that, execute requests (via Ajax) to the backend, in your PHP case, receive the response and display.

  

When do I know I should use javascript?

You can use all of your front end, as I said before, is just what many front-end frameworks do, however you MUST use when you need to do a user interaction, or something dynamic.

    
29.03.2018 / 23:05