Dumb question, but do I really have to do any validation for a form with only Checkboxes on it?
Also, do I need the concept of “stickiness”?
I’ve never worked with Checkboxes before, but it seems like they are binary - either you checked one or not - so what form validation would I need to do? (It’s not like a Textbox where you might display an error like “Comments can only be 1024 character long. Your response is 2000 character long!”)
As such, would I need a “Sticky Form” if the end user really can’t screw things up?
The solution I am working towards would be database driven and dynamic using arrays.
So you are saying I need to go in and somehow check that every checkbox has a valid value?
What I am confused about is that I don’t see where you would be displaying data validation errors to the end user like a regular form.
On my normal forms, I will display an error message in red if you enter a wrong value in a Textbox, for instance.
This script I am working on is just going to be a page of maybe 100 hobbies, where the user chooses their Top-5 Favorite Hobbies.
To me there doesn’t seem to be much to check, but then I have never used Checkboxes before, so I feel uncomfortable with this entire last-minute request…
You also mentioned “stickiness” in your other thread. In the same way, you build an array of the user interests and while looping through all interests, check if it is in the user interests array.
Thanks for the samples. As usual, I bit off more than I can chew. I have an idea of what I want to do, now it is just putting it all together. So much for having this done by suppertime?! (I probably just added another week onto things if I do this the way I want, but we’ll see…)
For checkboxes where you have predefined the valid values in the checkboxes themselves you could simply sanitize them rather than validating - just discard any that don’t have a valid value. The only time any would have an invalid value is if someone tampered with the form or the data being sent and so they already know the value they sent is invalid so you don’t need to tell them.
Either you checked an Interest and it is a Yes/TRUE/1 or you didn’t check it and it is a No/FALSE/0/NULL. Anything else is bogus, and I don’t feel compelled in that case to help you out, because you are likely a hacker!!
Yes, if you build an array like I did then use this to build your form as well as compare POST values you should be fine. If the “Junk” value is not in the array, it won’t be passed through to DB.
I think it should be the name of the interest. It gives you a unique value you can compare to on POST. if you put “Yes” or “TRUE” then you are going to need to figure out what this means on processing and without forced keys or some other way to identify the value it would be almost impossible as only selected checkboxes are passed and if they all say “true” then what? Use the name.
But remember, Drummin, I have name-interest[ ] so when the form gets submitted, each Checkbox that was checked would have a unique ID, right?
And the value= in this case wouldn’t need to be unique since there is a Key/ID to go along with it, right?
For example…
KEY VALUE
3 Yes
6 Yes
8 Yes
So it would seem it would be easier on my PHP to just iterate through the array and if look for “Yes”.
Although, thinking out loud, I believe the way Checkboxes work is that if you check them they get submitted in the $_POST array, and if they are unchecked then they never get sent over. Point being, just the presence of an item in the items array means that it was checked, regardless of whether it is “Yes” or “Football” or whatever, right??
I think either way of coding the inputs would be easy enough to do.
Either <input name="football" <input name="baseball"
or <input name="interest[]" value="football" <input name="interest[]" value="baseball"
I am basing my answer on the code you posted in post #3 where you have open keys, e.g. name=“interest”
IF these were input type=“text” then each input would be posted and each key would be incremental starting with zero. Checkboxes are only sent if checked, and again the keys would be incremental starting with zero.
So unless you are forcing keys, I don’t see how Baseball can have a key of 3, unless of course it was the fourth item on the list that was checked.
Probably the best way to see WHAT is being passed would be to print your POST.
Okay, I see where our “Communication Breakdown” was at!
Yes, since this morning I have created a table to stores all of Interests. Each has a unique ID. And when I populate my form, I will take the table data, stick it into an array, and then use the array to populate the Checkboxes on the form.
So regardless of which Checkboxes are chosen, then array containing the Checkbox responses - in the $_POST array - should look like I was saying in Post #14.
And the point I was trying to make was that the “Value” shouldn’t really matter if each Checkbox will always have the same Key/ID value, right?
So you now ARE forcing the KEY with an ID number… Is that right? Then if you have an ID number then it wouldn’t matter what value you used. If you are still using open keys then you still have a problem I was talking about. Hey, print_r($_POST) see if it returns what you expect.
Let’s say I have a universe of 10 Interests in my table. Each has a unique ID. When I build the Form, I query the database, get those 10 Interests with their IDs, then stick those in an array,and then use the array to populate the Checkboxes.
So, yes, each Checkbox has something like - I haven’t mastered the code yet…