chop Posted February 19, 2015 Share Posted February 19, 2015 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 More sharing options...
Larry Posted February 20, 2015 Share Posted February 20, 2015 foreach ($vegetables as $veg) { echo '<label><input type="checkbox" name="vegetables[]" value="' . $veg . '">' . $veg . '</label>'; } That should do it. Link to comment Share on other sites More sharing options...
chop Posted February 20, 2015 Author Share Posted February 20, 2015 Larry- Thank you so much... I'll try it and, if it works, study it for understanding. My biggest php headache is always the intermixing of it with HTML in forms. I worked for hours trying to get it! chop Link to comment Share on other sites More sharing options...
chop Posted February 20, 2015 Author Share Posted February 20, 2015 YESSS.. worked. And, I get it! Somehow I got stuck on $key => $value structure and thought I needed to use the $key within the loop. Way simpler now. chop Link to comment Share on other sites More sharing options...
Recommended Posts