Hybris Posted October 22, 2020 Share Posted October 22, 2020 Hi, I just started on a small project where I have a form where I want to prefill some stuff like the 2 last digits of the current year (20). I made a small javascript for that but have to put it in a function that I start onload in the <body> tag and it works just fine.. then it struck me I could write a similar function in php instead and I started to wonder what is best practice for small functions like these? Is there some general golden rule when its better to use javascript instead of php? (My feeling is php for database stuff and stuff thats happening as the page loads and javascript when a page already is loaded and one wants to mess with stuff like keypresses or mouse movements and things like that..?) Was several years since I did my last project so am a bit rusty.. Last time everyone told me to use Laravel instead of writing my own code so I did..or I tried..and tried..and tried.. but never understood what I was doing so I quickly lost interest so this time I will write everything from scratch using no frameworks. Quote Link to comment https://forums.phpfreaks.com/topic/311628-php-or-javascript-to-prefill-fields-on-a-page/ Share on other sites More sharing options...
Barand Posted October 22, 2020 Share Posted October 22, 2020 (edited) 1 hour ago, Hybris said: My feeling is php for database stuff and stuff thats happening as the page loads and javascript when a page already is loaded and one wants to mess with stuff like keypresses or mouse movements and things like that..? I agree with that approach. However, there may be cases where you have a JS function that is called as a result of user interaction and which also provides an initial value when the page loads. In that case it would make sense to call it on page load rather than duplicate the function in PHP. Horses for courses. Edited October 22, 2020 by Barand typo Quote Link to comment https://forums.phpfreaks.com/topic/311628-php-or-javascript-to-prefill-fields-on-a-page/#findComment-1582009 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.