Forum Moderators: mack
As far as I know there are certain things that javascript(client-side script) cannot do but php(server-side script)can. For example - processing of the input variables from forms.
And there are few things which can be done by using either of the two,that is, they can be done in javascript as well as in php. So in this case, if we don't need to hide the code from users, we can use javascript. But would it be ideal to do so?
Is it true that use of php puts additional load on the server which can otherwise be avoided by using javascript?
My overall question is :
What do you think should be the ratio of javascript code and php code for problems which can be coded in either ways? How do we decide if we are going to code a certain problem (which obviously comprises just a small part of any project) in javascript or php?
Client-side scripting can be enabled or may not be present in the browser (for example, search engine spiders.) For this reason, anything you do in Javascript must be duplicated or emulated by your server side programming or it is doomed to sporadic failure.
Example: shopping cart that calculates totals directly on the page using Javascript. If JS is disabled, when submitted you get an order for $300 of items but the total . . says $0. And that is the value that is passed to the payment processor. So your server-side programming - php, perl, asp, whatever - needs to replicate exactly what your Javascript does to insure proper calculations.
This, I think, invalidates and answers the question about "we can do the same thing with one or the other".
So to answer the question, Javascript or other client side processing is only essential in the sense that we use it to enhance the visitor's experience on our sites; to perform many of the tasks and speed up the processing of their quest without making an round trip top the server each time. Anything that relies on Javascript to function properly will eventually fail when a user agent comes along in which it's disabled.
Another reason to go for server side scripting is that it poses no problem with search engines. JavaScript is effectively thrown away by search engines (although I have seen some instances in my logfile where Googlebot fetched my .js files so they might parse it in some way) The result of a server side script is plain (X)HTML which is easily interpreted by all search engines.