Page 1 of 1

loop through checkbox array problem

Posted: Wed Jan 05, 2011 3:49 am
by helloise
this is my problem: i have a form with a list of products for a particular service and each product have two checkboxes1. graphic 2. text.. and a submit button
when the form first displays no chekboxes are checked obviously...the submit button does not display

now: as soon as i check any of the checkboxes the submit botton must display and when clicked it goes to another form where i do some things.

my code:

Code: Select all

<form action="<?php echo url_for('doc/document'); ?>" method="post"> 
<input type='hidden' name='service_id' value='<?php echo $service->getId() ?>'>

<table width='1000px' border='0' cellspacing='0'>
        <tr>
            <td class='td_header_2' align='left' colspan='2' width='50%'>
                Available products for service: <?php echo $service->getName()?>
            </td>   
        </tr>
        <tr>
         <table width='600px' class='tbl_content_box' border='0' cellspacing='0'>
            <tr>
                <td width='100px'>Products:</td>
                <td width='100px'>Graphic</td>
                <td width='100px'>Text</td>
            </tr>    
         </table>
        <table>
        <table width='600px' class='tbl_content_box' border='0' cellspacing='0'><?php 
            $product_names = LpmPagePeer::getByAllProdNames($service->getId()); [color=#0040BF]//i need this to get the products for the particular service
            foreach($product_names as $product_row) [/color]
            { 
            ?>   
            <tr>
                <td >&nbsp;</td><td width='200px'><?php echo $product_row->getName(); ?></td>
                <td width='200px'><input type="checkbox" name="graphic[]" value="<?php echo  $product_row->getId();?>" /></td>  [color=#0040FF]//and need this here to get the products selected id's(stored in graphic[]) to use in next form when submit button gets clicked..[/color]
                <td width='200px'><input type="checkbox" name="text[]"    value="<?php echo  $product_row->getId();?>" /></td> [color=#0040FF]//same for here[/color]
            </tr>
            <?php }         
            ?>
    	</table>
        <table>
            <tr>
                <td> 
                <?php
                    
                 [color=#FF4000]   //loop thgough the graphic[] and text[] arrays
                    //and if any gets checked, display the submit button
                    //have nop clue how to do this[/color]
                ?>
                </td>
            </tr>
        </table>
</form>
why i want to the two arrays before hand is because if NO checkboxes are selected and the user clicks submit button..i get errors on the next form because graphic[] and text[] will contain nothing..so i just want to check before hand

pleas help me?
thanks

Re: loop through checkbox array problem

Posted: Wed Jan 05, 2011 4:28 am
by jankidudel
You can do it using javascript

Re: loop through checkbox array problem

Posted: Wed Jan 05, 2011 10:08 am
by social_experiment
To cope with empty arrays (using PHP here, not JS)

Code: Select all

<?php
 if ( is_array($_POST['graphic']) && is_array($_POST['text']) ) {
  if (empty($_POST['graphic']) || empty($_POST['text']) ) {
   // one or both of the arrays are empty, error time.
  }
 //
 }
?>