0

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;
4
  • Is number of checkboxes the same per each category? Do you know number of categories in advance? Commented Jan 18, 2014 at 17:54
  • there are 'n' number of categories and also 'n' number of child checkboxes Commented Jan 18, 2014 at 18:02
  • 1
    Why does the while loop prevent you from changing the names? Anyway it looks like you could extract the table number from the first 2 characters of the value (t1,t2,etc.) Commented Jan 18, 2014 at 18:06
  • when t1col1 and t2col2 is checked then output of echo $query is: table1~t1col1,t2col1,-table2~t1col1,t2col1,- but expected output is table1~t1col1,-table2~t2col2,- Commented Jan 18, 2014 at 18:23

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

That sounds strange. If I remember it correctly, you could use keys in the name for a checkbox and get them back in php: name="tab[table1]" or name="tab[table1][]" - not shure which one works for you.
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.