Jump to content
Larry Ullman's Book Forums

Recommended Posts

assuming I create this array:

$vegetables=array('corn','squash','beans','carrots','peas','kale');

The following check boxes work fine in my form.

 <div class="checkbox">
<label><input type="checkbox"  name="vegetables[]" value="<?php echo $vegetables[0]?>"> <?php echo $vegetables[0]?></label>
      <label><input type="checkbox"  name="vegetables[]" value="<?php echo $vegetables[1]?>"> <?php echo $vegetables[1]?></label>
      <label><input type="checkbox"  name="vegetables[]" value="<?php echo $vegetables[2]?>"> <?php echo $vegetables[2]?></label>
      <label><input type="checkbox"  name="vegetables[]" value="<?php echo $vegetables[3]?>"> <?php echo $vegetables[3]?></label>
      <label><input type="checkbox"  name="vegetables[]" value="<?php echo $vegetables[4]?>"> <?php echo $vegetables[4]?></label>
      <label><input type="checkbox"  name="vegetables[]" value="<?php echo $vegetables[5]?>"> <?php echo $vegetables[5]?></label>    
    </div>

But I've been trying to go one step further so if I add another vegetable to $vegetables[] , I won't have to add another line of code in the check boxes. For example, if I add "spinach", I won't also have to add the line:

<label><input type="checkbox" name="vegetables[]" value="<?php echo $vegetables[6]?>"> <?php echo $vegetables[6]?></label>

I tried forever by using something along the lines of:

 

foreach ($vegetables as $key =>$value)

// mixing html and PHP here to create each of the 6 "<label> </label>" lines of code.

}

That way, all I have to do is add a vegetable to $vegetables and not change the html

 

I thought I had it at one point but I wasn't able to check more than one of the boxes at a time.

 

thanks for any comments

chop

Link to comment
Share on other sites

 Share

×
×
  • Create New...