My client wants me to create a list of about 20 “Interests” for users to review, but then they can only choose 5.
I want to use checkboxes, but am unsure of how to accomplish this.
So there would be 20 checkboxes listing things like this…
__ Football
__ Basketball
__ Baseball
__ Hockey
__ Soccer
:
:
__ Tennis
But you can only click up to 5 at any one time.
Is this possible using just PHP? (I don’t know client-side programming like JavaScript, AJAX, etc, so please keep responses to something I do know which is PHP!!)
My client’s website needs to be completed this weekend.
Now is NOT a good time to learn JavaScript!!!
Even if it is one line of code, it only confuses the issue.
Hopefully there is an easy way to do this in PHP, since almost anything can be done in PHP!
I just don’t know much about the Check-Box control, and how you would do this. (I do know how to set up radio buttons so you have single-exclusivity, but now how you’d say, "You can pick any 5 of the following 20 choices using checkboxes.)
In addition to wanting to avoid a debate on JavaScript versus PHP, the bigger question is understanding the mechanics of how Check-Boxes work, and how - in any language - you tell the HTML to behave a certain way.
In fact, maybe this deals entirely with HTML and not PHP…
The problem that you have is that the PHP magic all happens on the server but the user will be clicking checkboxes in his or her browser on his or her computer/tablet/smartphone (i.e. nowhere near your server where PHP resides). Pepster’s suggestion is really your only solution, but use <= 5 rather than just < 5.
If you need to restrict the user to only choosing up to 5 options in the browser, then you’re going to need that javascript on the client side (but still have to validate on the server anyway)
Yes, this is a perfect example of when JavaScript should be used.
No matter how prominent you have choose up to 5 only you know some won’t.
Having the form info travel to the server only to return an Error does not make for a pleasant user experience nor is it the most efficient use of server resources.
And then I guess I do a FOREACH on that array located in the $_POST and that’s where I can determine if there are more than 5 choices.
For now, I will have to send an error from the server if they select more than 5 - not exactly the end of the world in my book.
I’m sure JavaScript and AJAX will make things better when I learn them, but for now I have to use the things I do know and can manage in order to meet my deadline.
Again, though, the root of my question was understanding how the HTML works.
So it sounds like I am on the right track assigning an array to name=
Another related issue that is confusing me is what should go in the value=
In my example above I have value=“Football”, but if a Checkbox is a Boolean, then should it just contain something like Yes/TRUE/1?
(It seems my PHP would just want to check for a Boolean to do what it needs to do versus having to figure out that “Football” means the Football Checkbox was checked, but then I don’t know…)
But it’s not a boolean. If it was a single input then it could be. But in this case it’s a series of inputs where
value = not checked
or
value = “string”
If you wanted to you could have all separate inputs uniquely named, But as the options are being dynamically created from an array and they are a “group” having them be an array of inputs makes sense to me.
Looking at other scripts that I have written, I think I am close to figuring this out, but still could use some help.
So, my database stores all Interests and all Member_Interests (many-to-many).
I run a LEFT JOIN to give me all Interests and any that the given user chose, and then convert that into an array which is used to populate the form. (Each Checkbox has a unique ID and Name.
When the user submits the form, the $_POST array would contain an Interests array which would have a Key/Value pair.
It seems to me that the Value can be anything I want it to be, but since each Checkbox has a Key/ID associated with it, I could just make all of the Values equal to something simple like “Yes” or “TRUE”, right?
No, boolean in this case means checkbox will not appear in $_POST if it’s not checked.
But you can use any value.
For example, if you have empty text field (input type=“text”) in your form and user sends form, then in PHP there still be $_POST['inputname'] with empty value. But with checkbox, if it’s not checked, there will be nothing about this checkbox in $_POST.
Six of one, half a dozen of the other to be honest. I’d be tempted to suggest that using the id value as the array key assures the uniqueness of the actual value of interest in the resulting $_POST array whereas using an open ended array ‘’ could allow duplicate values to be sent to the server. Using
foreach(array_keys($_POST['interest'])...
isn’t really any more complex than using
foreach($_POST['interest']...
Given the OPs use case though, duplicate values wouldn’t get sent unless someone was tampering with the form post, which is a different kettle of fish.