Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Check all checkbox for array with key assigned

    I am working with a checkbox array where the key is already set. So instead of being able to use name="name[]", I need to use name="name[0]" etc.. I would like a button or a checkbox to select all in the array(name).

    The main reason for this is the nature of the checkbox. If a checkbox is not selected then it is completely skipped when looping through the array. To overcome this I added a hidden field of the same name which will assign a value even if the checkbox is not selected. Then the problem of using name[] arises.

    The form is populated with hundreds of entries using PHP so I am looking for a way to loop through them all.

    Here's a tiny snippet of HTML of how my form is set up.

    Any ideas appreciated.
    Code:
    <form action="update.php" method="post" enctype="multipart/form-data" name="myForm"> 
    <table class="list"> 
    <tr> 
    <td align="center"><strong></strong></td> 
    <td align="center"><strong>Current Name</strong></td> 
    <td align="center"><strong>Current Price</strong></td> 
    <td align="center"><strong>New Price</strong></td> 
    <td align="center"><strong>Difference</strong></td> 
    </tr> 
    <tr> 
    <td><input type="hidden" name="import_check[0]" value="no"  /><input type="checkbox" name="import_check[0]" value="yes" /><input type="hidden" name="id[0]" value="3"  /></td> 
    <td><input name="name[0]" type="hidden" id="name" value="Amano">Amano<input type="hidden" name="item_num[0]" value="0"  /><input name="new_item_num[0]" type="hidden" id="new_item_num" value="726452003771"></td> 
    <td><input name="price[0]" type="hidden" id="price" value="10.99">10.99</td> 
    <td><input name="new_price[0]" type="hidden" id="new_price" value="11.99">11.99</td> 
    <td>1.00</td> 
    </tr> 
    <tr> 
    <td><input type="hidden" name="import_check[1]" value="no"  /><input type="checkbox" name="import_check[1]" value="yes" /><input type="hidden" name="id[1]" value="4"  /></td> 
    <td><input name="name[1]" type="hidden" id="name" value="Angeline">Angeline<input type="hidden" name="item_num[1]" value="0"  /><input name="new_item_num[1]" type="hidden" id="new_item_num" value="729722700103"></td> 
    <td><input name="price[1]" type="hidden" id="price" value="11.99">11.99</td> 
    <td><input name="new_price[1]" type="hidden" id="new_price" value="12.99">12.99</td> 
    <td>1.00</td> 
    </tr>
    <tr> 
    <td colspan="5"><input type="submit" name="button" id="button" value="Submit"></td> 
    </tr>
    </table>
    </form>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    Ahh, the joys of working with PHP and it's silly way of mapping multiple values of the same name from form to PHP!

    But never fear, we can do it.

    Instead of using a hidden form field of the same name, do this:

    Upon submit of the page, simply loop through all the checkboxes. If one is *NOT* checked, then CHANGE its value to "no" and check it!

    So, a variation on your code:
    Code:
    <script>
    function fixForm(form)
    {
        for ( var ix = 0; ix < form.elements.length; ++ix )
        {
            var fld = form.elements[ix];
            if ( fld.name.substring(0,12) == "import_check" && ! fld.checked )
            {
                fld.value = "no";
                fld.checked = true;
            }
        }
        return true;
    }
    </scirpt>
    ....
    <form ... onsubmit="fixForm(this);">
    <input type="checkbox" name="import_check[0]" value="yes" />
    ...
    <input type="checkbox" name="import_check[1]" value="yes" />
    ...
    <input type="checkbox" name="import_check[2]" value="yes" />
    ...
    <input type=submit>
    </form>
    Will that do what you need?

    Now, there is a problem with that: If the user hits BACK, he/she may find all the checkboxes checked with no indication that the value has changed. You could of course fix that by again looping through all the checkboxes and UNCHEKING any with a value of "no" and then changing them back to "yes".

    But another option would be to use a *differently* named hidden field and have it capture the checked/unchecked state. Or, rather, use differently named checkboxes, which you will ignore in the PHP code.

    Example:

    Code:
    <script>
    function fixForm(form)
    {
        for ( var ix = 0; ix < form.elements.length; ++ix )
        {
            var fld = form.elements[ix];
            if ( fld.name.substring(0,15) == "cb_import_check" && ! fld.checked )
            {
                form.elements[fld.name.substring(3)] = fld.checked ? "yes" : "no";
            }
        }
        return true;
    }
    </scirpt>
    ....
    <form ... onsubmit="fixForm(this);">
    <input type="checkbox" name="cb_import_check[0]" value="yes" />
    <input type="hidden" name="import_check[0]" />
    ...
    <input type="checkbox" name="cb_import_check[1]" value="yes" />
    <input type="hidden" name="import_check[1]" />
    ...
    <input type="checkbox" name="cb_import_check[2]" value="yes" />
    <input type="hidden" name="import_check[2]" />
    ...
    <input type=submit>
    </form>
    One of those should work, no?
    Last edited by Old Pedant; 04-01-2009 at 06:55 AM.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    *sigh* Teach me not to read more carefully. You already have the unchecked button problem solved?? I'm a little surprised it works, using that code, but not too much so. PHP is weird in its handling of multiply-named fields.

    Okay, so it's pretty easy:
    Code:
    function checkAll( )
    {
        var form = document.NameOfForm;
        for ( var ix = 0; ix < form.elements.length; ++ix )
        {
            var fld = form.elements[ix];
            if ( fld.name.substring(0,12) == "import_check" ) fld.checked = true;
        }
    }

  • Users who have thanked Old Pedant for this post:

    MasterBeta (04-01-2009)

  • #4
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Wonderful! That is exactly what I was looking for. Thank you Old Pedant!
    I would have never thought to use substring.

    I added an option to check/uncheck all depending on the value of a single checkbox (checkEm). So now when you click the single checkbox it will select or un-select all checkboxes beginning with "import_check". I'll paste the entire code so you can have a look. I am horrible at javascript so maybe you will see a simpler method, but I think it works well. I'm not sure about using two if statements but wasn't sure. This time my html table is a bit larger for demonstration too.

    And yes PHP can be a cry baby sometimes. The hidden field trick is kind of ridiculus but it works.

    Thanks again.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Checkbox Test</title>
    
    <script language="javascript" type="text/javascript">
    //<![CDATA[
    <!-- Beginning of JavaScript -
    
    function checkUncheck()
    {
            if ( document.myForm.checkEm.checked == 1)
            {
                    var form = document.myForm;
                    for ( var ix = 0; ix < form.elements.length; ++ix )
                    {
                            var fld = form.elements[ix];
                            if ( fld.name.substring(0,12) == "import_check" ) fld.checked = true;
                    }
            }
            if ( document.myForm.checkEm.checked == 0)
            {
                    var form = document.myForm;
                    for ( var ix = 0; ix < form.elements.length; ++ix )
                    {
                            var fld = form.elements[ix];
                            if ( fld.name.substring(0,12) == "import_check" ) fld.checked = false;
                    }
            }
    }
    
    // - End of JavaScript - -->
    //]]>
    </script>
    </head>
    <body>
    <form action="update.php" method="post" enctype="multipart/form-data" name="myForm" id="myForm">
    <table class="list">
    <tr>
    <td align="center"><strong><input type="checkbox" name="checkEm" value="TRUE" onclick="checkUncheck()" /></strong></td>
    <td align="center"><strong>Current Name</strong></td>
    <td align="center"><strong>Current Price</strong></td>
    <td align="center"><strong>New Price</strong></td>
    <td align="center"><strong>Difference</strong></td>
    </tr>
    <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    </tr>
    <tr>
    <td><input type="hidden" name="import_check[0]" value="no" /> <input type="checkbox" name="import_check[0]" value="yes" /> <input type="hidden" name="id[0]" value="3" /></td>
    <td><input name="name[0]" type="hidden" value="Amano" />Amano <input type="hidden" name="item_num[0]" value="0" /> <input name="new_item_num[0]" type="hidden" value="726452003771" /></td>
    <td class="currency"><input name="price[0]" type="hidden" value="10.99" />10.99</td>
    <td class="currency"><input name="new_price[0]" type="hidden" value="10.99" />10.99</td>
    <td class="currency">0.00</td>
    </tr>
    <tr>
    <td><input type="hidden" name="import_check[1]" value="no" /> <input type="checkbox" name="import_check[1]" value="yes" /> <input type="hidden" name="id[1]" value="4" /></td>
    <td><input name="name[1]" type="hidden" value="Angeline Cabernet Sauvignon" />Angeline Cabernet Sauvignon <input type="hidden" name="item_num[1]" value="0" /> <input name="new_item_num[1]" type="hidden" value="729722700103" /></td>
    <td class="currency"><input name="price[1]" type="hidden" value="11.99" />11.99</td>
    <td class="currency"><input name="new_price[1]" type="hidden" value="11.99" />11.99</td>
    <td class="currency">0.00</td>
    </tr>
    <tr>
    <td><input type="hidden" name="import_check[2]" value="no" /> <input type="checkbox" name="import_check[2]" value="yes" /> <input type="hidden" name="id[2]" value="5" /></td>
    <td><input name="name[2]" type="hidden" value="Aquinas Cabernet Sauvignon" />Aquinas Cabernet Sauvignon <input type="hidden" name="item_num[2]" value="0" /> <input name="new_item_num[2]" type="hidden" value="833302001037" /></td>
    <td class="currency"><input name="price[2]" type="hidden" value="13.49" />13.49</td>
    <td class="currency"><input name="new_price[2]" type="hidden" value="13.49" />13.49</td>
    <td class="currency">0.00</td>
    </tr>
    <tr>
    <td><input type="hidden" name="import_check[3]" value="no" /> <input type="checkbox" name="import_check[3]" value="yes" /> <input type="hidden" name="id[3]" value="6" /></td>
    <td><input name="name[3]" type="hidden" value="Aquinas Napa Valley Merlot" />Aquinas Napa Valley Merlot <input type="hidden" name="item_num[3]" value="0" /> <input name="new_item_num[3]" type="hidden" value="833302001051" /></td>
    <td class="currency"><input name="price[3]" type="hidden" value="13.49" />13.49</td>
    <td class="currency"><input name="new_price[3]" type="hidden" value="16.99" />16.99</td>
    <td class="currency">3.50</td>
    </tr>
    <tr>
    <td><input type="hidden" name="import_check[4]" value="no" /> <input type="checkbox" name="import_check[4]" value="yes" /> <input type="hidden" name="id[4]" value="7" /></td>
    <td><input name="name[4]" type="hidden" value="Aquinas Pinot Noir" />Aquinas Pinot Noir <input type="hidden" name="item_num[4]" value="0" /> <input name="new_item_num[4]" type="hidden" value="833302001068" /></td>
    <td class="currency"><input name="price[4]" type="hidden" value="13.49" />13.49</td>
    <td class="currency"><input name="new_price[4]" type="hidden" value="15.99" />15.99</td>
    <td class="currency">2.50</td>
    </tr>
    <tr>
    <td><input type="hidden" name="import_check[5]" value="no" /> <input type="checkbox" name="import_check[5]" value="yes" /> <input type="hidden" name="id[5]" value="11" /></td>
    <td><input name="name[5]" type="hidden" value="Banrock Station Cabernet Sauvignon" />Banrock Station Cabernet Sauvignon <input type="hidden" name="item_num[5]" value="81434051448" /> <input name="new_item_num[5]" type="hidden" value="81434051448" /></td>
    <td class="currency"><input name="price[5]" type="hidden" value="5.99" />5.99</td>
    <td class="currency"><input name="new_price[5]" type="hidden" value="5.99" />5.99</td>
    <td class="currency">0.00</td>
    </tr>
    <tr>
    <td><input type="hidden" name="import_check[6]" value="no" /> <input type="checkbox" name="import_check[6]" value="yes" /> <input type="hidden" name="id[6]" value="12" /></td>
    <td><input name="name[6]" type="hidden" value="Banrock Station Chardonnay" />Banrock Station Chardonnay <input type="hidden" name="item_num[6]" value="81434044778" /> <input name="new_item_num[6]" type="hidden" value="81434044778" /></td>
    <td class="currency"><input name="price[6]" type="hidden" value="5.89" />5.89</td>
    <td class="currency"><input name="new_price[6]" type="hidden" value="5.89" />5.89</td>
    <td class="currency">0.00</td>
    </tr>
    <tr>
    <td><input type="hidden" name="import_check[7]" value="no" /> <input type="checkbox" name="import_check[7]" value="yes" /> <input type="hidden" name="id[7]" value="13" /></td>
    <td><input name="name[7]" type="hidden" value="Banrock Station Merlot" />Banrock Station Merlot <input type="hidden" name="item_num[7]" value="81434051585" /> <input name="new_item_num[7]" type="hidden" value="81434051585" /></td>
    <td class="currency"><input name="price[7]" type="hidden" value="5.89" />5.89</td>
    <td class="currency"><input name="new_price[7]" type="hidden" value="5.89" />5.89</td>
    <td class="currency">0.00</td>
    </tr>
    <tr>
    <td><input type="hidden" name="import_check[8]" value="no" /> <input type="checkbox" name="import_check[8]" value="yes" /> <input type="hidden" name="id[8]" value="14" /></td>
    <td><input name="name[8]" type="hidden" value="Banrock Station Riesling" />Banrock Station Riesling <input type="hidden" name="item_num[8]" value="81434024237" /> <input name="new_item_num[8]" type="hidden" value="81434024237" /></td>
    <td class="currency"><input name="price[8]" type="hidden" value="5.89" />5.89</td>
    <td class="currency"><input name="new_price[8]" type="hidden" value="5.89" />5.89</td>
    <td class="currency">0.00</td>
    </tr>
    <tr>
    <td><input type="hidden" name="import_check[9]" value="no" /> <input type="checkbox" name="import_check[9]" value="yes" /> <input type="hidden" name="id[9]" value="15" /></td>
    <td><input name="name[9]" type="hidden" value="Banrock Station Shiraz" />Banrock Station Shiraz <input type="hidden" name="item_num[9]" value="81434015853" /> <input name="new_item_num[9]" type="hidden" value="81434015853" /></td>
    <td class="currency"><input name="price[9]" type="hidden" value="5.99" />5.99</td>
    <td class="currency"><input name="new_price[9]" type="hidden" value="5.99" />5.99</td>
    <td class="currency">0.00</td>
    </tr>
    <tr>
    <td><input type="hidden" name="import_check[10]" value="no" /> <input type="checkbox" name="import_check[10]" value="yes" /> <input type="hidden" name="id[10]" value="16" /></td>
    <td><input name="name[10]" type="hidden" value="Banrock Station Shiraz/Cabernet Sauvignon" />Banrock Station Shiraz/Cabernet Sauvignon <input type="hidden" name="item_num[10]" value="81434012142" /> <input name="new_item_num[10]" type="hidden" value="81434012142" /></td>
    <td class="currency"><input name="price[10]" type="hidden" value="5.99" />5.99</td>
    <td class="currency"><input name="new_price[10]" type="hidden" value="5.99" />5.99</td>
    <td class="currency">0.00</td>
    </tr>
    <tr>
    <td colspan="6"><input type="hidden" name="count" value="91" /> <input type="hidden" name="FormSubmit" value="1" /> <input type="submit" name="Submit" value="Submit" /></td>
    </tr>
    </table>
    </form>
    </body>
    </html>


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •