Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Setup:

We've got a form with default text values present within the text box itself which "goes away" when I click on them and enter a value (not if I dont enter a value). Currently the 'default' value or the user inputted value gets POSTed to the PHP script.

Should I have the javascript code check if the value being submitted is the default one and set it to "" (blank string) OR should I perform this check in PHP where I'll have to do a check if its "" OR if its to the default value.

Which is more efficient, having javascript clean up the data before sending it to PHP, or having PHP do the check itself?

One reason to keep it within PHP (though not a consideration for us at the moment) is so that we can display the default text labels in multiple languages, and check if the user has left them blank. That would be easier in PHP, I imagine?

share|improve this question
    
You should consider the placeholder attribute, supported by all modern browsers (except IE) –  Dean Harding Aug 1 '11 at 8:21

3 Answers 3

up vote 10 down vote accepted

You need to do it on the server side w/ PHP. You cant rely that javascript was enabled on the client, or that nothing went wrong with it, or that the data sent to the server from the wilds of the interweb is in any way valid.

Ideally, you'd do it both in JavaScript on the client and in PHP on the server. Doing it in JavaScript might make the ui look more refined, but server-side validation and checking is a must.

share|improve this answer
    
"Doing it in JavaScript might make the ui look more refined" The purpose of client-side validation is instant feedback to the user, which in this case is not applicable or necessary. –  Raynos Aug 1 '11 at 7:14
    
Correct - but from a speed perspective, would it be faster for the javascript to 'blank it out' if its the default value, and then pass it to PHP? So I only have to do one check at the PHP level? Just trying to gain an understanding which approach would be faster... @GrandmasterB @Raynos? –  siliconpi Aug 1 '11 at 7:21
2  
@matt74tm there is no noticable speed difference but please go read about server-side validation and security. Remember all customers/clients/users are evil and will hack your website. Can't trust any of them –  Raynos Aug 1 '11 at 7:26

You should do both.

Having to wait for the form to submit back and PHP do the processing and then re-perform a whole page load just to tell you, you forgot to enter something in a box is very poor user experience.

Firstly validate your form inputs in JavaScript, either manually or by using a jQuery Validate plugin to speed things up for you.

Once your form submits, then perform the validation in PHP. Remember that JS can be disabled, tweaked as it is a client-side technology. Always rely on the PHP but never have it as a default else your users will be subject to a poor user experience.

Good luck with your validation.

Regards, Paul Dragoonis.

share|improve this answer
  • javascript for UX (user experience)

  • php for security

both is the better, but javascript with improving user experience in mind, and php for securing your app in mind

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.