loop through checkbox array problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
helloise
Forum Commoner
Posts: 31
Joined: Tue Sep 21, 2010 4:20 am

loop through checkbox array problem

Post 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
jankidudel
Forum Commoner
Posts: 91
Joined: Sat Oct 16, 2010 4:30 pm
Location: Lithuania, Vilnius

Re: loop through checkbox array problem

Post by jankidudel »

You can do it using javascript
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: loop through checkbox array problem

Post 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.
  }
 //
 }
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply