Jump to content

loop through checkbox array problem


helloise

Recommended Posts

i have this code:

foreach($product_names as $product_row) { ?>

            <tr>

                <td > </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>

                <td width='200px'><input type="checkbox" name="text[]"    value="<?php echo  $product_row->getId();?>" /></td>

            </tr>

            <?php }       

            ?>

    </table>

        <table>

            <tr>

                <td>

                      <input type='hidden' name='submit' value='Submit'> 

                <?php

                    if ( (isset($graphic)) || (isset($text) ))

                    {

                      echo "checkboxes checked";

                      //show submit button

                    }

                    else

                    {

                        //hide submit botton and

                        echo "no boxes check";

                    }

                ?>

                </td>

            </tr>

        </table>

   

so if i want to loop through grapic[] with the code:

line 36  $graphics_selected = $_POST['graphic'];

line 37  foreach($graphics_selected as $val)

            {

                      if there is something in array(thus any checkbox is checked) enable submit button

else if no checkboxes are checked disable submit button

          }

 

i get errors:

Notice: Undefined index: graphic in /home/helloises/svn_wappool/apps/lpmanager/modules/doc/templates/productSuccess.php on line 36 Warning: Invalid argument supplied for foreach() in /home/helloises/svn_wappool/apps/lpmanager/modules/doc/templates/productSuccess.php on line 37

 

please help????

Link to comment
Share on other sites

here are the whole .php code

 

<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());

            foreach($product_names as $product_row) { ?>

            <tr>

                <td > </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>

                <td width='200px'><input type="checkbox" name="text[]"    value="<?php echo  $product_row->getId();?>" /></td>

            </tr>

            <?php }       

            ?>

    </table>

        <table>

            <tr>

                <td>

                      <input type='hidden' name='submit' value='Submit'> 

                <?php

                $graphics_selected = $_POST['graphic'];

            foreach($graphics_selected as $val)

            {

                    echo "blablabl";

          }

                    if ( (isset($graphic)) || (isset($text) ))

                    {

                      echo "checkboxes checked";

                      //show submit button

                    }

                    else

                    {

                        //hide submit botton and

                        echo "no boxes check";

                    }

                ?>

                </td>

            </tr>

        </table>

   

</form>

Link to comment
Share on other sites

this is basically what your after needs a few tweaks to put your variable back in etcc and to hide the submit button again if there are no checked boxes.

 

<html>
<head>
    <script type="text/javascript">
    
    
    function check(){
        
        var graphics = document.getElementsByName("graphic[]");

        for(i=0;i<graphics.length;i++){
            
            if(graphics[i].checked){
                if(document.getElementById('submit').style.display=='none'){
                    document.getElementById('submit').style.display='block';
                }
            }
        }
        
        
        var text = document.getElementsByName("text[]");

        for(i=0;i<text.length;i++){
            
            if(text[i].checked){
                if(document.getElementById('submit').style.display=='none'){
                    document.getElementById('submit').style.display='block';
                }
            }
        }
        
        
        
    }
    </script>
</head>
<form action="#" method="post">
<input type='hidden' name='service_id' value='1'>

<table width='1000px' border='0' cellspacing='0'>
        <tr>
            <td class='td_header_2' align='left' colspan='2' width='50%'>
                Available products for service: bla 
            </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 = array(1=>"one",2=>"two");
            foreach($product_names as $k=>$v) { ?>
            <tr>
                <td > </td><td width='200px'>bla</td>
                <td width='200px'><input type="checkbox" name="graphic[]" value="<?=$k?>"  onclick="check()"/></td>
                <td width='200px'><input type="checkbox" name="text[]"    value="2" onclick="check()"/></td>
            </tr>
            <?php }         
            ?>
       </table>
        <table>
            <tr>
                <td>
                <input id="submit" type='submit' name='submit' value='Submit' style="display:none">
                <?php
                 if($_POST['graphic']){
                    
                    $graphics_selected = $_POST['graphic'];
                    
                    foreach($graphics_selected as $val)
                    {
                        echo "bla";
                    }
                 }
                ?>
                </td>
            </tr>
        </table>
     
</form>


Link to comment
Share on other sites

thank you for the trouble but:

i added the code but have no products displayed now cos i dont have the lines:

$product_names = LpmPagePeer::getByAllProdNames($service->getId());

foreach($product_names as $product_row)

{

<td > </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>

                <td width='200px'><input type="checkbox" name="text[]"    value="<?php echo  $product_row->getId();?>" /></td>

}

 

i also need the $product_row->getId() cos when i hit the submit button i to go to the next form i need the id's of the products(kept in graphic[] and text[]) that where checked to get other info for display on the next form

 

yikes i dont know now..i am a newbie so have no idea on how to go about it...please help

thanks!

Link to comment
Share on other sites

thank you yet again, but im lost now....would you mind giving me the full code of how it should be so i can go through it and try to understand it better please???  it will be much appreciated!

 

sorry for the trouble but im still learning all this stuff...

many thanks

Link to comment
Share on other sites

<html>
<head>
    <script type="text/javascript">
   
   
    function check(){
       
        var graphics = document.getElementsByName("graphic[]");

        for(i=0;i<graphics.length;i++){
           
            if(graphics[i].checked){
                if(document.getElementById('submit').style.display=='none'){
                    document.getElementById('submit').style.display='block';
                }
            }
        }
       
       
        var text = document.getElementsByName("text[]");

        for(i=0;i<text.length;i++){
           
            if(text[i].checked){
                if(document.getElementById('submit').style.display=='none'){
                    document.getElementById('submit').style.display='block';
                }
            }
        }
       
       
       
    }
    </script>
</head>
<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());
            foreach($product_names as $product_row) { ?>
            <tr>
                <td > </td><td width='200px'>bla</td>
                <td width='200px'><input type="checkbox" name="graphic[]" value="<?php echo  $product_row->getId();?>"  onclick="check()"/></td>
                <td width='200px'><input type="checkbox" name="text[]"    value="<?php echo  $product_row->getId();?>" onclick="check()"/></td>
            </tr>
            <?php }         
            ?>
       </table>
        <table>
            <tr>
                <td>
                <input id="submit" type='submit' name='submit' value='Submit' style="display:none">
                <?php
                 if($_POST['graphic']){
                    
                    $graphics_selected = $_POST['graphic'];
                    
                    foreach($graphics_selected as $val)
                    {
                        echo $val."\n";
                    }
                 }
                ?>
                </td>
            </tr>
        </table>
     
</form>

Link to comment
Share on other sites

thank you thank you!!! its working...now just one last thing:

 

how can i check if graphic[] OR text[] contains nothing, thus array is empty???

also the code:

if($_POST['graphic'])

{                   

        $graphics_selected = $_POST['graphic'];          //is line 62

        foreach($graphics_selected as $val)

        {

              echo $val."\n";

        }           

}

give error:

 

Undefined index: graphic in /home/helloises/svn_wappool/apps/lpmanager/modules/doc/templates/productSuccess.php on line 62

 

thanks very much

 

Link to comment
Share on other sites

im not sure why your getting the undefined index as i dont have that problem when i run the code. if you want to check on the php side you can use the empty function or javascript use the length function. post ur full code for me to see with regards to that index problem

Link to comment
Share on other sites

the full text is exactly like you gave it to me :) everything works perfect..

 

i was thinking maybe i can set a variable(boolean) in the javascript part and check that later in the code???(but dont know how to do that though)  just to see if the two arrays contain something..because if one of them contain nothing and i hit submit i get errors on the second form :)

<html>
<head>
    <script type="text/javascript">
        function check()
        {
            var graphics = document.getElementsByName("graphic[]");
             
            for(i=0;i<graphics.length;i++)
            {
                if(graphics[i].checked)
                {
                    [color=red]var gchecked = true;[/color]
                    if(document.getElementById('submit').style.display=='none')
                    {
                        document.getElementById('submit').style.display='block';
                    }
                }
            }
            var text = document.getElementsByName("text[]");
            for(i=0;i<text.length;i++)
            {
                if(text[i].checked)
                {
                    [color=red]var tchecked = true;[/color]
                    if(document.getElementById('submit').style.display=='none')
                    {
                        document.getElementById('submit').style.display='block';
                    }
                }
            }  
        }
    </script>
</head>


<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 width='600px' class='tbl_content_box' border='0' cellspacing='0'><?php
            $product_names = LpmPagePeer::getByAllProdNames($service->getId());
            foreach($product_names as $product_row) { ?>
            <tr>
                <td > </td><td width='200px'><?php echo  $product_row->getName();?></td>
                <td width='200px'><input type="checkbox" name="graphic[]" value="<?php echo  $product_row->getId();?>"  onclick="check()"/></td>
                <td width='200px'><input type="checkbox" name="text[]"    value="<?php echo  $product_row->getId();?>" onclick="check()"/></td>
            </tr>
            <?php }         
            ?>
       </table>
        <table>
            <tr>
                <td>
                <input id="submit" type='submit' name='submit' value='Submit' style="display:none">
                <?php
               [color=red]     ///if isgchecked or istchecked is false then i must display a warning[/color]
                ?>
                </td>
            </tr>
        </table>
</form>


 

thanks for all your help so far..just this one last hurdle :)

Link to comment
Share on other sites

ha ha haa the thing is if say for instance only hte graphics array contain checked boxes and the text array doesnt thus the submit button will show, then when i hit the submit button, the text array i passed to the next form is empty and i then will get an error...hope it makes sense...so i need to get around this somehow :) but dont know how..

 

some more help please???

thanks!

Link to comment
Share on other sites

change the javascript function check i gave you to this instead its overkill but it will work.

 


   
   
    function check(){
        
        var none_checked = false;
        var graphics = document.getElementsByName("graphic[]");
        var text = document.getElementsByName("text[]");
        
        for(i=0;i<graphics.length;i++){
            if(!graphics[i].checked){
               none_checked = true;
            }
        }
        
        for(i=0;i<text.length;i++){
            if(!text[i].checked){
               none_checked = true;
            }
        }
        
        if(none_checked == true){
            document.getElementById('submit').style.display='none';
        }else{
            document.getElementById('submit').style.display='block';
        }
        
        for(i=0;i<graphics.length;i++){
           
            if(graphics[i].checked){
                if(document.getElementById('submit').style.display=='none'){
                    document.getElementById('submit').style.display='block';
                }
            }
        }
       
        for(i=0;i<text.length;i++){
           
            if(text[i].checked){
                if(document.getElementById('submit').style.display=='none'){
                    document.getElementById('submit').style.display='block';
                }
            }
        }
       
       
       
    }
   

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.