views:

73

answers:

1

Good evening. I am relatively new to programming and have spent untold hours trying to resolve by issue with checkboxes. What I am trying to figure out is how to code the following:

  • I want the user of my form to be able to select anywhere from 1 - 4 events and be able to store in a mysql database what those selections are. For instance, if event 1 is checked then store a 1, and if not then store a 0. I understand that this requires the use of a foreach loop, or something similar, but I can't seem to figure out how to implement it.

  • When I get to the point where I either have to edit the information or just approve the information prior to allowing it to be viewed on the website, I now need to be able to 're-check' the box on the form based upon how it was originally set by the customer. Again, I have a basic understanding of how to do this, but...

Somewhere out there in the ether there must exist a site that can help me understand how to accomplish these tasks. If you know of one, please let know the URL so I can learn and accomplish my goal.

Thanks for your help,

Dennis

+1  A: 

I want the user of my form to be able to select anywhere from 1 - 4 events and be able to store in a mysql database what those selections are. For instance, if event 1 is checked then store a 1, and if not then store a 0. I understand that this requires the use of a foreach loop, or something similar, but I can't seem to figure out how to implement it.

Give checkboxes each the same name but a different value.

<input type="checkbox" name="events" value="1"> event 1<br>
<input type="checkbox" name="events" value="2"> event 2<br>
<input type="checkbox" name="events" value="3"> event 3<br>
<input type="checkbox" name="events" value="4"> event 4<br>

The $_GET['events'] will then only return the checked values in an array.

When I get to the point where I either have to edit the information or just approve the information prior to allowing it to be viewed on the website, I now need to be able to 're-check' the box on the form based upon how it was originally set by the customer. Again, I have a basic understanding of how to do this, but...

You need to set the checked attribute based on the request parameter.

$events = $_GET['events']; // Don't forget to do prechecks and sanitize magic quotes.
// ...

<input type="checkbox" name="events" value="1" <?php echo (in_array(1, $events) ? 'checked' : '') ?>>
<input type="checkbox" name="events" value="2" <?php echo (in_array(2, $events) ? 'checked' : '') ?>>
// ...

In other words, if 1 is available (checked) in $events, then just print checked attribute which will make the checkbox checked. You can of course print it all in a foreach loop if you've the checkbox labels and initial values in an assocative array.

BalusC
Thanks, Balus, but your code gives the same result as what I had - no way to acknowledge an unchecked condition. I fixed that by assigning a place in the array to each event, for instance events[0], events[1], and so on, and then using a for loop to see whether or not that event had been checked. Now I have the first part of my issue solved. Thanks to your snippet, I think that I can solve the second half. Thanks again.
Uhm, unchecked values are indeed not sent to the server side. But you already know all available values in the server side. Thus, simply do `allvalues - checkedvalues = uncheckedvalues`.
BalusC
They are now! I probably set it up wrong from the beginning, but I have four fields in the database, to individually monitor the 4 events. When I prepare to display which events are included in the tournament, I need to know which are selected and which aren't. So, by sending a 0 if the box isn't checked and a 1 when it is, I now know.