[UPHPU] Checkboxes and MySQL
Mac Newbold
mac at macnewbold.com
Tue Nov 28 17:01:45 MST 2006
Today at 1:13pm, Nicholas Vettese said:
> <script>
> if (isset($_POST['submitted'])) { // Handle the form.
> require_once('mysql_connect-local.php');
> foreach ($test as $m) {
> $query = "INSERT INTO movies (test) VALUES ('$m')";
> $result = mysql_query ($query) or trigger_error("Query: $query\n<br
> />MySQL Error: " . mysql_error());
> }
> }
> ?>
As Wade mentioned, you'll want to check for "submit" instead of
"submitted". Second, if you've got Register Globals on, you should turn it
off. When it is off, you'll need to look for $_POST['test'] or
$_REQUEST['test'] instead of $test for your foreach loop. You might also
want to add something like this:
if (!isset($_REQUEST['test'])) { $_REQUEST['test'] = array(); }
That will prevent you from getting an error when they don't check any
boxes.
When you're to the point where you want to pre-check any checkboxes they
already checked, you'll want a loop for that too, so you can check the
database and decide which rows get a "checked" attribute in them. Note
that it would be very easy to set up an array like so:
$boxes = array("sci-fi"=>"Science Fiction","vintage"=>"Vintage"...);
Then do something like this to print each one:
foreach ($boxes as $val => $name) {
$c = (isset($checked[$val]) ? "checked" : "");
echo "<input type='checkbox' name='test[]' value='$val' $c />$name<br />\n";
}
Mac
> <h1>Checkbox Testing</h1>
> <p>Please tell us which type of movies you like</p>
> <form action="movies.php" method="post">
> <input type="checkbox" name="test[]" value="vintage" />Vintage<br />
> <input type="checkbox" name="test[]" value="sci-fi" />Scince Fiction<br
> />
> <input type="checkbox" name="test[]" value="documentary"
> />Documentary<br />
> <input type="checkbox" name="test[]" value="comedy" />Comedy<br />
> <input type="checkbox" name="test[]" value="family" />Family<br />
> <input type="checkbox" name="test[]" value="drama" />Drama<br />
> <input type="checkbox" name="test[]" value="adult" />adult<br />
>
> <input type="submit" name="submit" value="Submit" />
> <input type="hidden" name="hidden" value="TRUE" />
> </form>
> </script>
>
> Thanks in advance for any help with this.
>
> Nick
>
> _______________________________________________
>
> UPHPU mailing list
> UPHPU at uphpu.org
> http://uphpu.org/mailman/listinfo/uphpu
> IRC: #uphpu on irc.freenode.net
>
--
Mac Newbold MNE - Mac Newbold Enterprises, LLC
mac at macnewbold.com http://www.macnewbold.com/
More information about the UPHPU
mailing list