Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use client-side or server-side? [closed]

I just finished an intro to web dev course in my CS program and came away wondering something simple. When should you use JavaScript (client-side) instead of server-side (we used PHP but anything applies) code? Vice-versa as well.

like image 986
Portaljacker Avatar asked Dec 21 '11 07:12

Portaljacker


2 Answers

There is no recipe for deciding that. A few notes:

  • security and validation should always be present at the server side (sometimes duplicated in the client).
  • the client-side should contain only UI-logic. No business logic.
  • logically, everything that accesses a database should be on the server.

Of course, if your application is a RIA (rich internet app), then you can have logic on the client. So it all depends.

like image 119
Bozho Avatar answered Sep 29 '22 17:09

Bozho


Javascript should be only used to manipulate the UI of the page. You can also do certain validations using it, however, there must be corresponding validation on the server-side. For doing any data manipulation, applying business logic, etc you should always use server side code.

Here are some cases where you will use client-side code:

  • Changing the look (UI) of the page e.g. dynamically show/hide some elements
  • Validate user inputs (this should also be done on server side)

Cases where to use server-side code:

  • Validation of user inputs (should always be done on server side irrespective of whether done on client side or not.)
  • User authentication
  • Business logic (deciding what to show to which users, calculations)
  • Database access
like image 25
Virendra Avatar answered Sep 29 '22 17:09

Virendra