Posted by Hilarion on 09/13/05 13:35
>> I would transfer the result of a checkbox selection into an SQL request.
>>
>> Could you help me ?
>>
>> Many thanks in advance
>
>
>
> <form method="post" action="formhandler.php">
> <input type="hidden" name="cb" value="0" />
> <input type="checkbox" name="cb" value="1" />
> <input type="submit" name="submit" value="Just submit it!" />
> </form>
>
> In formhandler.php:
>
> <?php
> $checkboxstate = $_POST['cb'];
> $query = "INSERT INTO tablename (fieldname) VALUES ($checkboxstate)";
> // run $query;
> ?>
>
> Very basic example. Needs some adjusting to suit your needs but you'll get
> the basic idea...
I wouldn't try this. You used hidden field as some kind of default
value (in case the checkbox is not checked and no value from checkbox
is passed), but the order of passing values by the browser is not
defined (different browsers may use different ordering algorithm) and
the order in which PHP parses values is also not defined. This means
that $_POST['cb'] can have '0' value even if the checkbox WAS checked.
You should simply use checkbox input and check in the script if the
$_POST['cb'] is set or not. If you want to avoid situation when
someone enters "formhandler.php" script URL manualy and the script
acts as if the form was submited without the checkbox being checked,
then use hidden field with some different name and check if that
one is set at the start of "formhandler.php" (if it is, then the
form was submited, if not, then someone entered the URL manualy).
You can also use this hidden field to control state of the form.
Hilarion
[Back to original message]
|