Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

PHP, Loop, Check Boxes

  • 25-02-2011 11:32AM
    #1
    Registered Users, Registered Users 2 Posts: 378 ✭✭


    Hi guys

    I would be greatfull with some help please

    I run an sql query, then in a while loop I ouput and create a table with one column of check boxes

    I then have a javascript function that I need to select all the checkboxes or uncheck them

    This funcation works if I only return one row in my while look, but any more than one row and the "select all" doesnt select anything

    I would be ever so greatfull for any help

    Here is the code:

    Javascript:

    <script type="text/javascript">
    <!--
    function checkall()
    {
    var df=document.InboxForm.elements;
    for(var i=0;i<df.length;i++)
    {
    //alert(df.name);
    df.checked=true;
    }
    }

    function uncheckall()
    {
    var df=document.InboxForm.elements;
    for(var i=0;i<df.length;i++)
    {
    //alert(df.name);
    df.checked=false;
    }
    }
    -->
    </script>


    While loop:

    while($row = mysql_fetch_array($result))
    {
    $subject=$row;
    $datesent=$row;
    $fromuser=$row;
    $from_firstname=$row;
    $from_lastname=$row;
    $id=$row;
    $read=$row;

    echo'





    <tr '.$RowColor.'>
    <td align="center"><form name="InboxForm"><input type="checkbox" name ="selected[]"></form></td>

    <td align="center" style="font-weight: '.$new.'; color:'.$new_color.';">'.$from_firstname.' '.$from_lastname.' ('.$fromuser.')</td>


    <td style="word-wrap: break-word; font-weight: '.$new.'; color:'.$new_color.';" align="center">'.$subject.'</td>
    <td align="center" style="font-weight: '.$new.'; color:'.$new_color.'";>'.$datesent.'</td>

    <td align="center" style="font-weight: '.$new.'; color:'.$new_color.';"><a href="view-message.php?id='.$id.'"><img src="images/view-message.png"></a></td></tr>';

    }


    Then call the function to check all the check boxes:

    echo '
    <a href="#" onclick="checkall(InboxForm);" title="Check all boxes">CheckAll</a>';



    Thanks in advance of any replies


Comments

  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    Firstly, did you alert the length that it's getting ?

    Then try giving each checkbox a unique ID - leaving the NAME as is

    Failing that, use jQuery - it'll do this with its eyes closed using one line of code.


  • Registered Users, Registered Users 2 Posts: 378 ✭✭bob2oo7


    Sorted with Jquery, thank you for the help


Advertisement