开发者

Using checkboxes

开发者 https://www.devze.com 2022-12-21 23:38 出处:网络
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:

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开发者_JAVA技巧 know the URL so I can learn and accomplish my goal.

Thanks for your help,

Dennis


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消