[FX.php List] Checkbox

Allyson Olm allyson at fmwebschool.com
Mon Apr 3 09:47:46 MDT 2006


Hi Rob,

I usually use eregi() to determine whether the array contains the value list
value.

foreach($vlResult['valueLists']['contact_type'] as $key => $value){
if(eregi($value,$searchData['contact_type'][0])){
$selected = 'checked';

}else{

$selected = '';

}
?>

<input type="checkbox" name="type" value="<?php echo $value; ?>"<?php echo
$selected; ?>><?php echo $value; ?>

<?php } ?>

In kindness,
Allyson Olm
FMWebschool
http://www.fmwebschool.com
-----Original Message-----
From: fx.php_list-bounces at mail.iviking.org
[mailto:fx.php_list-bounces at mail.iviking.org] On Behalf Of Steve Winter
Sent: Sunday, April 02, 2006 9:42 PM
To: 'FX.php Discussion List'
Subject: RE: [FX.php List] Checkbox

Rob,

I'm a little lost here, but it looks to me like you're trying to compare a
scalar variable '$value' with an array '$TRK' in the statement if ($TRK ==
$value), which in my understanding of php isn't going to return the result
you expect...

If I've got this right $TRK is an array which contains all of the values
which have been selected, is that correct...? 

Give this a try and see if it works for you;

<?php foreach($Vis2ListResult['valueLists']['Trykkeri'] as $key=>$value){ ?>

    <input type="checkbox" name="Trykkeri" value="<?php echo $value; ?>"
<?php if(in_array($value, $Data['Trykkeri'][0])) echo '"checked"'; ?> />
    <?php echo $value; } ?>

Note however that this is also going to result in you losing data when this
is posted back to the db because you're using the name 'Trykkeri' for each
of the check boxes, so whichever is the last checkbox selected is what that
variable will enter when posted back to the db.

If you want to have an array which stores all of the checked values use
this;

<?php foreach($Vis2ListResult['valueLists']['Trykkeri'] as $key=>$value){ ?>

    <input type="checkbox" name="Trykkeri[]" value="<?PHP echo $value; ?>"
<?php if(in_array($value, $Data['Trykkeri'][0])) echo '"checked"'; ?> />
    <?php echo $value; } ?>

Note the addition of the [] in the name="Trykkeri[]" section...you'll then
end up with all the values being posted back to your php page, at which
point you can write them back to your db...

Hope this helps...let me know if not, or if something doesn't make sense...

Cheers
Steve

-----Original Message-----
From: fx.php_list-bounces at mail.iviking.org
[mailto:fx.php_list-bounces at mail.iviking.org] On Behalf Of Rob H.
Christensen
Sent: Monday, 3 April 2006 12:38 a.m.
To: fx.php_list at mail.iviking.org
Subject: [FX.php List] Checkbox

I have some checkboxes, which can have multiple values. How do I show the
checked boxes on the page?

I have tried this:
<?PHP
$TRK=$Data['Trykkeri'][0];
foreach($Vis2ListResult['valueLists']['Trykkeri'] as $key=>$value){
            if($TRK==$value){ $Select="checked";}else{$Select="";} ?>
            <input type="checkbox" name="Trykkeri" value="<?PHP echo $value;
?>""<?PHP echo $Select;?>"><?PHP echo $value;
            }?>

This shows the checked box as long as there is only one checked. As soon as
more options are checked, it only shows empty checkboxes.

$TRK is an array, which correctly displays the checked boxes.

Any suggestions?

Rob


_______________________________________________
FX.php_List mailing list
FX.php_List at mail.iviking.org
http://www.iviking.org/mailman/listinfo/fx.php_list




_______________________________________________
FX.php_List mailing list
FX.php_List at mail.iviking.org
http://www.iviking.org/mailman/listinfo/fx.php_list






More information about the FX.php_List mailing list