I’m attempting learn a better way to post checkboxes into variables, the way I used to do it was long and tedious as I used to post each checkbox into its own variable, like this.
I had previously set up a loop that used $i to add numbers to values, similar to the example i’ll post below, but had trouble actually assigning the values to individual variables, I think the $i was left over perhaps, I’m not 100% sure, i’ve been trying to solve this problem on and off for a day or two now.
Where abouts would I be able to find some decent reading on how arrays work? I have the concept I think, just not the know-how to use them correctly in PHP.
Also could you explain how your example will work? as I’ll need to have the checkboxes assigned to individual variables
Righto, I’ll try out yours. The reason I require them in separate variables is because the information is going to be mailed to different people, depending on what is checked. I was planning on just using a few basic ‘if’ statements further on. Is there a better way of doing this that I’m not aware of?
I’d recommend using array keys. If you use and refer to checkboxes as first,second, third etc and you decide to change the order in the markup then your backend code will start silently referring to the wrong checkboxes. Also if not all are checked the alignment will go wacky.
I assume that by the example above (making minor modifications),
$hearDog = $_POST['hearDog'];
if(isset($_POST['hearDog'])) {
foreach($_POST['hearDog'] as $key => $value) {
}
if (empty($hearDog)) {
$error['hearDog'] = 'Please choose how you heard about this particular dog(s)';
}
}
and the user chooses ‘Breed Rescue Site’ and ‘Pets911’, then the email will post this:
How did you hear about this particular dog(s)?
Breed Rescue Site
Pets911
It’s close. The foreach will want to be able to handle multiple $key and $value entries. If only one should be chosen and handled, you should use radio choices instead of checkboxes.
No, it doesn’t look about right.
You need an article on programming basics too.
Looping through an array is very similar to looking in your pockets for the coin.
Checking first pocket… Empty.
Checking another pocket… Found!
Checking third pocket… Empty.
Imagine you’re making note “Not found at all” if pocket was empty. What it would say? Not found. But you’ve got one! So, something wrong with algorithm.
Try to think about it.
But you don’t need loop at all. You can just check if variable not empty.
But.
Not inside loop, because it makes no sense.
Existing variable. Where variable $hearDog come from?
So, checking would be just 3 lines:
if (empty($_POST['hearDog'])) {
$error['hearDog'] = 'Please choose how you heard about this particular dog(s)';
}
And for the array-to-text transformation you can use very useful implode() function.
$hearDog = implode(", " , $_POST['hearDog']);
if (empty($hearDog)) {
$error['hearDog'] = 'Please tell us how you heard about this particular dog(s)?';
}
RESULT:
2. How did you hear about this particular dog(s)?
Breed Rescue Site,
Except I get the extra comma, even if there’s only one selection (by the way, I plan on converting this to radio buttons, but I believe this to be useful later on in my form). Any way there’s a function to exclude the comma if there’s only one selection or when there’s a last selection? Or am I asking for something more complicated?