PDA

View Full Version : How do checkboxes work?


JJR512
03-18-2002, 07:30 AM
I can't figure out how checkboxes work. What I want to do is have a list of unique items, each item with a checkbox, so that the user can select whatever items he/she wants. Then he/she clicks a submit button, and whatever items were checked by the user get stored into the database.

How do you write both the HTML for the checkbox, form, and submit button (and whatever else is needed), and the PHP code that figures out which items were checked?

Admin
03-18-2002, 10:17 AM
First of all, how do you want to store the selections? Does every item have an ID of its own?

JJR512
03-18-2002, 04:55 PM
Each item will be the name of a website, and each item's unique ID will be the URL to the website. The URLs for the checked items will get stored in a custom table that has two columns, userid and urls, so the URLs so should be joined together (separated by spaces, for later explosion) and stored in one field.

Admin
03-19-2002, 05:23 AM
Hope this helps. :)
<?php

if ($_POST['action'] != 'update') {

/* get the data from the database
* let's say $row['urls'] has the data for now */

$selarray = explode(' ', $row['urls']);

/* now just echo all checkboxes for all urls
* for the checkbox, use this:
* (assuming $currenturl has the url of the current checkbox) */
echo '<input type="checkbox" name="urlarray[]" value="'.htmlspecialchars($currenturl).'"'.iif(in_array($currenturl, $selarray), ' checked', '').'>';

} else {

$urldata = implode(' ', $urlarray);
/* update the table with userid and urls with $urldata */

}