Page 1 of 1
Javascript and Radio Button Verification
Posted: Tue Jun 23, 2009 12:51 pm
by lmg
I have been googling and searching through the forums but haven't found exactly what I am looking for. What I have right now is a set of questions using radio buttons. I was trying to do error handling via php, but things weren't working, so I figured I would try some javascript.
This is what I would like to happen in my form:
User enters information --- User pushes 'submit' -----User missed a question - page with errors displayed OR User entered all information - proceed to next page
If the user misses a question, I would like the page to reload with their answers intact, a general error message at the top of the page, and an error message above each question which was not answered. What I would like is very similar to
this page (just push sign in without entering information).
To make this even easier, I would ideally like to implement a script which will cause an error message to pop up if the user has missed a question (ie: user enters answers for question 1 and 3 but misses 2, so an error message will appear on question 2 as soon as an answer to question 3 is selected).
I hope this makes sense.
Can anyone point me in the right direction for this? Can I even do this with Javascript?
TIA for your help!
Re: Javascript and Radio Button Verification
Posted: Tue Jun 23, 2009 3:26 pm
by patrickmvi
The link to google that you sent appears to do it server side. It's really more a matter of preference as to how you decide to do it. If you choose to do it server side, then you would need to save of the results of the form submittion to then re-populate the form on reload, if you do it via JS, then you'd have to basically write a condition for each question. Do you have an example of what this form you're trying to do this for looks like?
Re: Javascript and Radio Button Verification
Posted: Tue Jun 23, 2009 3:30 pm
by lmg
This is sort of what it looks like. There will be 20-28 questions per page though.
Re: Javascript and Radio Button Verification
Posted: Tue Jun 23, 2009 10:09 pm
by patrickmvi
The link you posted doesn't appear to work.
Re: Javascript and Radio Button Verification
Posted: Wed Jun 24, 2009 5:30 pm
by lmg
Sorry about that.
This link should work.
Re: Javascript and Radio Button Verification
Posted: Wed Jun 24, 2009 7:36 pm
by califdon
I think I would do it in Javascript, but all your examples seem to be server-side. What you do is, instead of using the standard <input type=submit... button, use a plain <input type=button onClick='mysubmit();'> button and write a Javascript function called mysubmit(). In that function, you check each form element that you want to validate and if ALL validations are OK, you issue a command like document.myform.submit(). If any validation fails, you never execute that line, but for each element that fails, you do whatever you're going to do to that element. My favorite way is to change the border color property of the element to red (and maybe thicker), then have a single alert() box or other message area that says "Please check items with red borders."
Re: Javascript and Radio Button Verification
Posted: Thu Jun 25, 2009 10:13 am
by pickle
Doing the validation server-side or client-side isn't really a choice. You pretty much have to do it server-side to ensure it gets done. If you rely on client-side validation, what happens if I turn off Javascript? Then I can mess up your form in all sorts of delightful ways.
Javascript should only be used to enhance the user experience, not define it. Get your server-side validation working first, then move on to client-side.
Re: Javascript and Radio Button Verification
Posted: Thu Jun 25, 2009 10:54 am
by lmg
So are you saying that I should be doing the validation with php and then showing the user what is wrong with javascript? I have extremely limited knowledge of javascript and not much more with php

Re: Javascript and Radio Button Verification
Posted: Thu Jun 25, 2009 1:49 pm
by califdon
pickle wrote:Doing the validation server-side or client-side isn't really a choice. You pretty much have to do it server-side to ensure it gets done. If you rely on client-side validation, what happens if I turn off Javascript? Then I can mess up your form in all sorts of delightful ways.
Javascript should only be used to enhance the user experience, not define it. Get your server-side validation working first, then move on to client-side.
I'm sure that's good advice, but just to be contrarian, I'd like to comment that there are exceptions, in my opinion.
I agree that you really have to have server-side validation to protect the integrity of your data, but often it's desirable to have,
in addition, client-side validation that gives user-friendly corrections without refreshing the page. Any client-side validation should degrade gracefully in case JS is disabled (and my example was a bad one, relying on the JS function to submit). The fact is that only a small proportion of users disable JS (the last figures I saw estimated that 5% of users do so) and, although you should not ignore 5% of your visitors, sometimes it may be worth the benefits to 95% of your visitors to provide a JS solution, with a carefully thought-out degrade path for the 5%.
To the OP: With you having only a limited knowledge of both Javascript and PHP, it's hard to advise you on how to program a non-trivial application. As I said above, I agree with ~pickle's statement that server-side validation with PHP is necessary, to protect the integrity of the data you are going to store in a database. I was describing something beyond that, that I have done on a couple of projects, in Javascript, as a user convenience. If you're just beginning to program, and you're doing this yourself, I think my suggestions may be not very helpful to you. In this case, you probably should do all your validation with PHP when the form is submitted and before you store anything in the database. You will need to design your form so that, in case of validation problems, you can send the acceptable data items back to the same form page they originally saw, leaving the invalid items blank, and perhaps unhide an error message that tells them to fix the specific problems.
Re: Javascript and Radio Button Verification
Posted: Fri Jun 26, 2009 4:19 pm
by lmg
I guess I will just have to see what I can do then

Thanks.