Page 1 of 3 123 LastLast
Results 1 to 10 of 30

Thread: Select All checkbox

  1. #1
    Join Date
    Jul 2011
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Question Select All checkbox

    Is it possible to create a "Select All" check-box in a PHP program without using Javascript?

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    PHP operates on the server. I have no idea what you mean by a "Select All" check-box, but I'm guessing no. Only Javascript can interact with the user when the webpage is active.

    You could use PHP to generate a checkbox (the HTML element) and even add some Javascript commands to it, but the PHP itself won't create any sort of "selecting" action.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    What about adding an additional checkbox labeled "all"?

    If "all" is selected along with any other checkboxes the "all" checkbox will override the other checked off boxes.

    I am not entirely sure this is what you are looking for though.
    To choose the lesser of two evils is still to choose evil. My personal site

  4. #4
    Join Date
    Jul 2011
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Hi james
    I didn't get what you said.Can you please write the code?

    p.s.: What I mean is that I want to create 5 checkboxes beside 5 files . Then there show be a a checkbox below the 5 files called "Select All" ,which one being selected ,selects /checks the 5 checkboxes. Hope you all get me now

  5. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Unfortunately you would need javascript.
    To choose the lesser of two evils is still to choose evil. My personal site

  6. #6
    Join Date
    Apr 2012
    Location
    Chester, Cheshire
    Posts
    329
    Thanks
    7
    Thanked 35 Times in 35 Posts

    Default

    It's a very simple script to make. Here's a working example:

    HTML 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>
        <!-- Page Title -->
        <title>Select All Checkboxes Example</title>
        <!-- Meta Block -->
        <meta content="text/html; charset=iso-8859-1" http-equiv="content-type" />
        <meta content="Select All Checkboxes Example" name="description" />
        <meta content="Select All, Checkboxes, Example" name="keywords" />
        <meta content="all,index,follow" name="robots" />
        <meta content="noodp" name="msnbot" />
        <meta content="global" name="distribution" />
        <!-- Javascript Scripts -->
        <script type="text/javascript">
        //<![CDATA[
    
            function selectAllFiles(c) {
                for (i = 1; i <= 5; i++) {
                    document.getElementById('chkFile' + i).checked = c;
                }
            }
    
        //]]>
        </script>
    </head>
    <body>
        <div id="page">
            <p>
                <input id="chkFile1" type="checkbox" title="File 1" />File 1</p>
            <p>
                <input id="chkFile2" type="checkbox" title="File 2" />File 2</p>
            <p>
                <input id="chkFile3" type="checkbox" title="File 3" />File 3</p>
            <p>
                <input id="chkFile4" type="checkbox" title="File 4" />File 4</p>
            <p>
                <input id="chkFile5" type="checkbox" title="File 5" />File 5</p>
            <p>
                <input id="chkAllFiles" type="checkbox" title="All Files" onchange="selectAllFiles(this.checked);" />All
                Files</p>
        </div>
    </body>
    </html>

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    If you don't mind reloading the page, you could have a button on it that would check all. It would be the submit button of a separate form that would get or post to the page itself data telling it to render all the boxes as checked. You could also have a check none button that would also be a separate form and work in a similar manner.

    But any other data would have to be preserved somehow. If there were no other data or if all other data was already in session and not subject to change on that page, that would be fine. Otherwise, it wouldn't work well without javascript anyway.

    What's the big problem with javascript? You can use it, and if there's none available on the user's end, you can make the check all checkbox unavailable.
    Last edited by jscheuer1; 06-16-2012 at 02:58 PM. Reason: add proviso
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Apr 2012
    Location
    Chester, Cheshire
    Posts
    329
    Thanks
    7
    Thanked 35 Times in 35 Posts

    Default

    Here's a working version in PHP.

    default.php:
    PHP Code:
    <?php

        
    function checked() {
            if(isset(
    $_POST['chkAllFiles'])) {
                return 
    'checked="checked" ';
            }
            else
            {
                return 
    '';
            }
        }

    ?>

    <!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>
        <!-- Page Title -->
        <title>Select All Checkboxes Example</title>
        <!-- Meta Block -->
        <meta content="text/html; charset=iso-8859-1" http-equiv="content-type" />
        <meta content="Select All Checkboxes Example" name="description" />
        <meta content="Select All, Checkboxes, Example" name="keywords" />
        <meta content="all,index,follow" name="robots" />
        <meta content="noodp" name="msnbot" />
        <meta content="global" name="distribution" />
    </head>
    <body>
        <div id="page">
            <form action="default.php" method="post">
            <p>
                <input name="chkFile[]" type="checkbox" title="File 1" <?php echo checked(); ?>/>File 1</p>
            <p>
                <input name="chkFile[]" type="checkbox" title="File 2" <?php echo checked(); ?>/>File 2</p>
            <p>
                <input name="chkFile[]" type="checkbox" title="File 3" <?php echo checked(); ?>/>File 3</p>
            <p>
                <input name="chkFile[]" type="checkbox" title="File 4" <?php echo checked(); ?>/>File 4</p>
            <p>
                <input name="chkFile[]" type="checkbox" title="File 5" <?php echo checked(); ?>/>File 5</p>
            <p>
                <input name="chkAllFiles" type="checkbox" title="All Files" <?php echo checked(); ?> />
                    <input name="btnGo" type="submit" value="All Files" /></p>
            </form>
        </div>
    </body>
    </html>

  9. The Following User Says Thank You to ApacheTech For This Useful Post:

    megha (06-17-2012)

  10. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Yes, but now the form is virtually useless for any other purpose.

    This will do sort of the same thing, and when it's done, including optionally changing one or more checkboxes after checking them all or clearing them all, the actual form may be submitted to another page (processing.php in this example):

    PHP Code:
    <?php
        $checked 
    = isset($_POST['chkAllFiles'])? 'checked' '';
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <!-- Page Title -->
        <title>Select All Checkboxes Example</title>
        <!-- Meta Block -->
        <meta content="text/html; charset=utf-8" http-equiv="content-type" />
        <meta content="Select All Checkboxes Example" name="description" />
        <meta content="Select All, Checkboxes, Example" name="keywords" />
    </head>
    <body>
        <div id="page">
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                <input name="chkAllFiles" type="submit" value="All Files"/>
        </form>
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                <input name="unchkAllFiles" type="submit" value="No Files"/>
        </form>
            <form action="processing.php" method="post">
            <p>
                <input name="chkFile[]" type="checkbox" title="File 1" <?php echo $checked?>/>File 1</p>
            <p>
                <input name="chkFile[]" type="checkbox" title="File 2" <?php echo $checked?>/>File 2</p>
            <p>
                <input name="chkFile[]" type="checkbox" title="File 3" <?php echo $checked?>/>File 3</p>
            <p>
                <input name="chkFile[]" type="checkbox" title="File 4" <?php echo $checked?>/>File 4</p>
            <p>
                <input name="chkFile[]" type="checkbox" title="File 5" <?php echo $checked?>/>File 5</p>
            <p>
                    <input type="submit" value="Submit" /></p>
            </form>
        </div>
    </body>
    </html>
    But there's still the problem of, what about other data if any in the main form? If there is any, like -say, username - that won't survive the reloads that check and uncheck the boxes unless it's in and drawn from the session and cannot be changed on this form.
    Last edited by jscheuer1; 06-16-2012 at 05:46 PM. Reason: add info on processing.php
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  11. #10
    Join Date
    Apr 2012
    Location
    Chester, Cheshire
    Posts
    329
    Thanks
    7
    Thanked 35 Times in 35 Posts

    Default

    So there's no options in PHP for AutoPostBack, like in ASP.NET? Or any runat:"server" equivalent?

    This is possible in ASP.NET without using Javascript, but the OP was asking specifically for PHP.

    If you can use ASP.NET, you can do this without using JavaScript, as it has AutoPostBack features added in.

    default.aspx:
    HTML Code:
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    
    <!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>
        <!-- Page Title -->
        <title>Select All Checkboxes Example</title>
        <!-- Meta Block -->
        <meta content="text/html; charset=iso-8859-1" http-equiv="content-type" />
        <meta content="Select All Checkboxes Example" name="description" />
        <meta content="Select All, Checkboxes, Example" name="keywords" />
        <meta content="all,index,follow" name="robots" />
        <meta content="noodp" name="msnbot" />
        <meta content="global" name="distribution" />
    </head>
    <body>
        <form id="Form1" runat="server">
        <asp:ScriptManager ID="AJAXHandler" runat="server" EnablePartialRendering="true">
        </asp:ScriptManager>
        <div id="page">
            <asp:UpdatePanel ID="AjaxForm" runat="server">
                <ContentTemplate>
                    <asp:CheckBoxList ID="chkFiles" runat="server">
                        <asp:ListItem Text="File 1" />
                        <asp:ListItem Text="File 2" />
                        <asp:ListItem Text="File 3" />
                        <asp:ListItem Text="File 4" />
                        <asp:ListItem Text="File 5" />
                    </asp:CheckBoxList>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="chkAllFiles" EventName="CheckedChanged" />
                </Triggers>
            </asp:UpdatePanel>
            <asp:CheckBox ID="chkAllFiles" Text="All Files" runat="server" AutoPostBack="true" />
        </div>
        </form>
    </body>
    </html>
    default.aspx.vb:
    Code:
    Option Explicit On
    Option Strict On
    
    Partial Class _Default
        Inherits System.Web.UI.Page
    
        Protected Sub chkAllFiles_CheckedChanged(sender As Object, e As System.EventArgs) Handles chkAllFiles.CheckedChanged
            For Each chk As ListItem In chkFiles.Items
                chk.Selected = chkAllFiles.Checked
            Next
        End Sub
    
    End Class
    Last edited by jscheuer1; 06-16-2012 at 07:38 PM. Reason: post merge

Bookmarks

Posting Permissions

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