samoht Posted July 1, 2008 Share Posted July 1, 2008 hello all, I am having a little problem with a form for gift cards that I made. here is a snippet where the checkboxes are: <td colspan="2"><center> Check as many boxes as you need, <br> or write in the denomination that you choose. </center></td> </tr> <tr> <?php // set checked field(s) for checkboxes $mark = array('50' => '', '75' => '', '100' => '', '125' => '', '150' => '', '200' => '', '250' => '', '300' => '', '500' => '', '750' => '', '1000' => ''); foreach ($mark as $k => $v ) { if (sizeof($mark) == 0 || !in_array( $_POST['checkbox'], $mark)) { $mark[$k] = "checked='checked'"; } elseif ( !$edit ) { $mark[$k] = "disabled='disabled'"; } } ?> <td colspan="2" class="noborders"> <input type="checkbox" name="checkbox[]" value="50" style="border:0;" <?php echo $mark['50'];?> /> $50 <input type="checkbox" name="checkbox[]" value="75" style="border:0;" <?php echo $mark['75'];?> /> $75 <input type="checkbox" name="checkbox[]" value="100" style="border:0;" <?php echo $mark['100'];?> /> $100 <input type="checkbox" name="checkbox[]" value="125" style="border:0;" <?php echo $mark['125'];?> /> $125 <input type="checkbox" name="checkbox[]" value="150" style="border:0;" <?php echo $mark['150'];?> /> $150 <input type="checkbox" name="checkbox[]" value="200" style="border:0;" <?php echo $mark['200'];?> /> $200 <br> <input type="checkbox" name="checkbox[]" value="250" style="border:0;" <?php echo $mark['250'];?> /> $250 <input type="checkbox" name="checkbox[]" value="300" style="border:0;" <?php echo $mark['300'];?> /> $300 <input type="checkbox" name="checkbox[]" value="500" style="border:0;" <?php echo $mark['500'];?> /> $500 <input type="checkbox" name="checkbox[]" value="750" style="border:0;" <?php echo $mark['750'];?> /> $750 <input type="checkbox" name="checkbox[]" value="1000" style="border:0;" <?php echo $mark['1000'];?> /> $1,000 </td> </tr> <tr> <td colspan="2"><span >Other Amount: $</span> <input name="otheramount" type="text" value="<?php if(isset($_POST['otheramount'])) echo $_POST['otheramount'];?>" size="4" <?php echo $readonly;?>> <span >.00</span><br> <br> </td> The problem I am having is that I pass through this form 2 times - the first time to fill it out and the second to confirm (also with the option to edit) on the second go around every one of my check boxes is marked?? Any ideas about what is wrong with my code?? thanks Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 1, 2008 Share Posted July 1, 2008 looks like you can clean up the code a bit...try this out: <td colspan="2"><center> Check as many boxes as you need, <br> or write in the denomination that you choose. </center></td> </tr> <tr> <td colspan="2" class="noborders"> <?php // set checked field(s) for checkboxes $amounts = array(50,75,100,125,150,200,250,300,500,750,1000); foreach ($amounts as $amount) { echo '<input type="checkbox" name="checkbox[]" value="'.$amount.'" style="border:0;" '; if (is_array($_POST['checkbox']) && in_array($amount,$_POST['checkbox'])) echo 'checked="checked" '; elseif ( !$edit ) echo 'disabled="disabled"'; echo ' /> $'.$amount.' '; } ?> </td> </tr> <tr> <td colspan="2"><span >Other Amount: $</span> <input name="otheramount" type="text" value="<?php if(isset($_POST['otheramount'])) echo $_POST['otheramount'];?>" size="4" <?php echo $readonly;?>> <span >.00</span><br> <br> </td> Quote Link to comment Share on other sites More sharing options...
samoht Posted July 1, 2008 Author Share Posted July 1, 2008 Yes, that is much nicer! thanks Arron, will this work with only one value in the array? - I suppose it will but I wondered about using is_array()?? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 1, 2008 Share Posted July 1, 2008 in which array? $_POST['checkbox'] ?? yes it will...the in_array is there to make sure values are there. cus if $_POST isn't an array (ie no POST at all or just no checkboxes) then in_array will throw an error. if only one checkbox is checked, $_POST['checkbox'] will still be an array (ie the code will still work) Quote Link to comment Share on other sites More sharing options...
samoht Posted July 1, 2008 Author Share Posted July 1, 2008 very cool, thanks for the help and explanation. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.