Jump to content

[SOLVED] checkboxes on form array problem


samoht

Recommended Posts

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

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.