Sharad Kale
Sharad Kale

Reputation: 971

how read dynamically generated checkbox values

Following checkbox structure generated dinamically through PHP while loop, so checkbox name is same for all checkbox name="cols[]"

    <li>
<input type="checkbox" name="tab" value="table1"/>
<ul>
    <li><input type="checkbox" name="cols[]" value="t1col1"</li>
    <li><input type="checkbox" name="cols[]" value="t1col2"</li>
    <li><input type="checkbox" name="cols[]" value="t1col3"</li>
</ul>
</li>
<li>
<input type="checkbox" name="tab" value="table2"/>
<ul>
    <li><input type="checkbox" name="cols[]" value="t2col1"</li>
    <li><input type="checkbox" name="cols[]" value="t2col2"</li>
    <li><input type="checkbox" name="cols[]" value="t2col3"</li>
</ul>
</li> 

problem is when I read value as bellow in php I unable to differentiate the values of array cols[] according to its parent categories. what is the solution for this situation

if(isset($_POST['tab']))      
{
    foreach($_POST['tab'] as $tabs_entry)
        {
            $query.=$tabs_entry.'~';
                     if(isset($_POST['cols']))
                     {
                        foreach($_POST['cols'] as $cols_entry)
                            $query.$cols_entry.',';
                        $query.="-";
                     }
         }

 }
 echo $query;

Upvotes: 0

Views: 777

Answers (2)

user3177179
user3177179

Reputation:

solved by using same checkbox name array for table as well as checkboxes and can split parent values and child values by adding saperator "~"(tilde) and ","(comma)

    <li>
<input type="checkbox" name="tab[]" value="table1~"/>
<ul>
    <li><input type="checkbox" name="tab[]" value="t1col1,"/></li>
    <li><input type="checkbox" name="tab[]" value="t1col2,"/></li>
    <li><input type="checkbox" name="tab[]" value="t1col3,"/></li>
</ul>
</li>
<li>
<input type="checkbox" name="tab[]" value="table2~"/>
<ul>
    <li><input type="checkbox" name="tab[]" value="t2col1,"/></li>
    <li><input type="checkbox" name="tab[]" value="t2col2,"/></li>
    <li><input type="checkbox" name="tab[]" value="t2col3,"/></li>
</ul>
</li>

Upvotes: 1

D. Melo
D. Melo

Reputation: 2239

I could sove it with simple adjustments in the form and an additional IF in the inner loop.

Maybe this will sove:

<form action="tab-cols.php" method="post">
    <ul>
     <li>
    <!-- Changed the name from tab to tab[], so everyone survives -->
    <input type="checkbox" name="tab[]" value="table1"/>
    <ul>
        <li><input type="checkbox" name="cols[]" value="t1col1"</li>
        <li><input type="checkbox" name="cols[]" value="t1col2"</li>
        <li><input type="checkbox" name="cols[]" value="t1col3"</li>
    </ul>
    </li>
    <li>
    <input type="checkbox" name="tab[]" value="table2"/> 
    <ul>
        <li><input type="checkbox" name="cols[]" value="t2col1"></li>
        <li><input type="checkbox" name="cols[]" value="t2col2"></li>
        <li><input type="checkbox" name="cols[]" value="t2col3"></li>
    </ul>
    </li>
    <ul>
<input type="submit" value="lalala">
</form>
<pre>
<?
$query ="  "; //added this

print_r($_POST);

if(isset($_POST['tab']))      
{
$t = 1; //added this
    foreach($_POST['tab'] as $tabs_entry)
        {
            $query.=$tabs_entry.'~';
             if(isset($_POST['cols']))
             {
                foreach($_POST['cols'] as $cols_entry){
            if(preg_match("/t".$t."(.*)/", $cols_entry) ){ //added this IF, so we can diferentiate
                        $query = $query.$cols_entry.',';                
            }

        }
                $query.="-";
             }
         $t++; //added this
         }
 }
 echo $query;

Upvotes: 0

Related Questions