Will the code below work when it comes to associating hidden inputs with buttons?
<p>I need some help with when it comes to displaying the correct hidden inputs with the associated buttons</p>

<p>I have an application which you can view <a href="http://helios.hud.ac.uk/u0867587/Mobile_app/QandATable2.php" rel="nofollow">here</a></p>

<p>Please follow steps below in order to use application:</p>

<ol>
<li>Click on the "Open Grid" link and select button "5", you will see 5 buttons "A-E" appear below.</li>
<li>Click on button 'A' and 'C', these buttons would turn green meaning that they have been turned on.</li>
<li>Now click on the "Add question" button to append what you have done into a table row below.</li>
<li>Now please repeat steps 1-3 again but this time choose button "7" so you will see buttons "A-G" and click on buttons 'B', 'D' and 'F' so that these buttons are turned on.</li>
</ol>

<p>Now you will see that there are 2 table rows, first table row has buttons 'A' and 'C' turned on and second row has buttons 'B', 'D' and 'F' turned on.</p>

<p>Now what I want to do is only post (using the $_POST method) the buttons which are turned on only. Now I find out that you can't post buttons using the $_POST method but what you can do is create hidden input fields for each button and post them, if button is on then value of hidden input is 1, if it is off then value is 0.</p>

<p>But I am not sure if I am coding this correctly so what my question is that can somebody review the code below and state if my code below will do the job or does it need some tweaking in order for it to work? Im worried that the hidden inputs are not correct with the associated buttons:</p>

<p>Now below is the code which outputs the letter buttons and their hidden inputs for top option and answer control:</p>

<pre><code> &lt;table id="optionAndAnswer" class="optionAndAnswer"&gt;
    &lt;tr&gt;
        &lt;table id="answerSection"&gt;
            &lt;tr&gt;

        &lt;?php
            $i = 1;
            foreach($a as $key =&gt; $val){
                if($i%7 == 1) echo"&lt;tr&gt;&lt;td&gt;";
                echo"&lt;input type=\"button\" onclick=\"btnclick(this);\" value=\"$val\" id=\"answer".$val."\" name=\"answerName[$val]\" class=\"answerBtns answers answerBtnsOff\"&gt;";    
                echo"&lt;input type=\"hidden\" value=\"0\" id=\"hiddenAnswer".$val."\" name=\"hidden[$val]\" class=\"offButtons\"&gt;";   
                if($i%7 == 0) echo"&lt;/td&gt;&lt;/tr&gt;";
                $i++;
            }
        ?&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
        &lt;td&gt;
        &lt;input class="answerBtns answers answerBtnsOff" name="answerName[True]"  id="answerTrue"    type="button"   value="True"    onclick="btnclick(this);"/&gt;
        &lt;input class="offButtons" name="hidden[True]" id="hiddenAnswerTrue" value="0" type="hidden"/&gt;
        &lt;input class="answerBtns answers answerBtnsOff" name="answerName[False]" id="answerFalse"   type="button"   value="False"   onclick="btnclick(this);"/&gt;
        &lt;input class="offButtons" name="hidden[False]" id="hiddenAnswerFalse" value="0" type="hidden"/&gt;
        &lt;input class="answerBtns answers answerBtnsOff" name="answerName[Yes]"   id="answerYes"     type="button"   value="Yes"     onclick="btnclick(this);"/&gt;
        &lt;input class="offButtons" name="hidden[Yes]" id="hiddenAnswerYes" value="0" type="hidden"/&gt;
        &lt;input class="answerBtns answers answerBtnsOff" name="answerName[No]"    id="answerNo"      type="button"   value="No"      onclick="btnclick(this);"/&gt;
        &lt;input class="offButtons" name="hidden[No]" id="hiddenAnswerNo" value="0" type="hidden"/&gt;
        &lt;/td&gt;
        &lt;/tr&gt;
        &lt;/table&gt;
        &lt;/td&gt;
        &lt;/tr&gt;
        &lt;/table&gt;
</code></pre>

<p>Below is the code where it outputs the letter buttons and hidden inputs from the top control and appends into the table rows (A user can turn on and off letter buttons in a table row if they wish):</p>

<pre><code>    function insertQuestion(form) {   

                var context = $('#optionAndAnswer');
        var currenttotal = context.find('.answerBtnsOn').length;        



        var $tbody = $('#qandatbl &gt; tbody'); 
        var $tr = $("&lt;tr class='optionAndAnswer' align='center'&gt;");
        var $td = $("&lt;td class='extratd'&gt;");
        var $answer = $("&lt;div class='answer'&gt;3. Answer:&lt;br/&gt;&lt;/div&gt;");

        var $this, $row, $cell;
        $('#optionAndAnswer .answers').each(function(i, v) {
            $this = $(this);
            if(i%7 == 0) {
                $row = $("&lt;tr/&gt;").appendTo($answer);
                $cell = $("&lt;td/&gt;").appendTo($row);
            }

            var v = $this.val();
            var a = { name:$this.attr('name'), value:$this.val(), class:$this.attr('class'), id:$this.attr('id')+'Row' };
            var h = $this.is(':visible')?'inline-block':'none'
            var $newBtn = $("&lt;input type='button' style='display:" + h + "' onclick='btnclick(this);' /&gt;").attr(a);

            var $newHdn = $("&lt;input type='hidden' value='0' id='hiddenAnswer" + v  + "' name='hidden[" + v + "]' class='offButtons' /&gt;");

            $newBtn.appendTo($cell);
            $newHdn.appendTo($cell);
        });


        $tr.append($td);
        $td.append($answer);
        $tbody.append($tr); 



    }
</code></pre>

<p>Finally below is the code where it turns on and off the letter buttons and their associated hidden inputs:</p>

<pre><code>    function btnclick(btn)
    {
        var context = $(btn).parents('#optionAndAnswer');
        if (context.length == 0) {
            context = $(btn).parents('tr');
        }
        var $btn = $(btn);
        var value = btn.value;

    $(btn).toggleClass("answerBtnsOff");
    $(btn).toggleClass("answerBtnsOn");

        var hiddenId = '#hiddenAnswer'+btn.value;
        if ( $(btn).hasClass("answerBtnsOff") )
        {
            $(hiddenId).val('0');
            $(hiddenId).toggleClass('offButtons');
            $(hiddenId).toggleClass('onButtons');       
        }
        else
        {
            $(hiddenId).val('1');
            $(hiddenId).toggleClass('onButtons');
            $(hiddenId).toggleClass('offButtons');
        }   

        $('.answertxt', context).val(context.find('.answerBtnsOn').length &gt; 0 ? context.find('.answerBtnsOn').length : 0);

        return false;
    }
</code></pre>
