Jump to content

php or javascript to prefill fields on a page?


Recommended Posts

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.

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 by Barand
typo
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.