Tick all checkboxes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Morrison

    Tick all checkboxes

    Hi,

    I have a set of 4 checkboxes and I want to have a button that will check all
    the boxes. I have looked on Google but can only find examples of it being in
    Javascript. Is there any way of doing it in a php file. If not, how do I go
    about using a Javascript file? Do I have to make a reference to it in the
    php?

    Cheers,

    Paul


  • Jerry Stuckle

    #2
    Re: Tick all checkboxes

    Paul Morrison wrote:[color=blue]
    > Hi,
    >
    > I have a set of 4 checkboxes and I want to have a button that will check all
    > the boxes. I have looked on Google but can only find examples of it being in
    > Javascript. Is there any way of doing it in a php file. If not, how do I go
    > about using a Javascript file? Do I have to make a reference to it in the
    > php?
    >
    > Cheers,
    >
    > Paul
    >
    >[/color]

    Paul,

    PHP is server side, not client side. You can do it with PHP - but that
    would mean submitting the file back to PHP and regenerating the entire
    page with the checkboxes checked.

    Much easier to do it in Javascript, which is client-side. And no, you
    don't have to reference the javascript in the PHP file.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Gordon Burditt

      #3
      Re: Tick all checkboxes

      >I have a set of 4 checkboxes and I want to have a button that will check all[color=blue]
      >the boxes. I have looked on Google but can only find examples of it being in
      >Javascript. Is there any way of doing it in a php file. If not, how do I go[/color]

      PHP runs on the server. It doesn't run until after you submit the
      form or click on a link.
      [color=blue]
      >about using a Javascript file? Do I have to make a reference to it in the
      >php?[/color]

      You'd need to find a client-side way of doing it. I don't think it's
      possible in just HTML. Javascript could do it.

      Gordon L. Burditt

      Comment

      • d

        #4
        Re: Tick all checkboxes

        "Paul Morrison" <nospam@nospam. com> wrote in message
        news:dsdoh4$4th $1@oheron.kent. ac.uk...[color=blue]
        > Hi,
        >
        > I have a set of 4 checkboxes and I want to have a button that will check
        > all the boxes. I have looked on Google but can only find examples of it
        > being in Javascript. Is there any way of doing it in a php file. If not,
        > how do I go about using a Javascript file? Do I have to make a reference
        > to it in the php?
        >
        > Cheers,
        >
        > Paul[/color]

        Javascript is the way for that. Using PHP is very possible, but javascript
        is quicker to implement and for the browser.

        dave


        Comment

        • mannerboy

          #5
          Re: Tick all checkboxes

          here is a simple example,wish it can be helpful! :)
          <script>
          function CheckAll(form){
          for(var i=0;i<form.elem ents.length;i++ ){
          var e = form.elements[i];
          if (e.name == 'id[]'){
          e.checked = form.chkall.che cked;
          }
          }
          }
          </script>

          <form name="test" method="post" >
          <input name="chkall" type="checkbox"
          onclick="CheckA ll(this.form);" >Select All
          <input type="checkbox" name="id[]" value="1000">Jo e Schmoe<br />
          <input type="checkbox" name="id[]" value="1001" checked="checke d"><br
          />
          <input type="checkbox" name="id[]" value="1002">Ja ne Johnson<br />
          <input type="checkbox" name="id[]" value="1003">Ch arlie Brown<br />
          </form>

          Comment

          • Paul Morrison

            #6
            Re: Tick all checkboxes

            Thanks guys, got that sorted now, I didnt realise that you could put
            Javascript into a php file.

            I now have another problem with the checkboxes. I set up my form as follows:

            <form method="get" action="sub.php ">
            <div align="left" style="font-size:12px ;clear : both">
            <input type=button name="CheckAll" value="Check All"
            onClick="modify _boxes(true,4)" style="font-size:10px"/>
            <input type=button name="UncheckAl l" value="Uncheck All"
            onClick="modify _boxes(false,4) " style="font-size:10px"/><p/>
            <div style="width:10 px"></div><input type="checkbox"
            name="subscript ion" value="economic " id="economic"/><label
            for="economic"> Economic</label><br/>
            <div style="width:10 px"></div><input type="checkbox"
            name="subscript ion" value="environm ental" id="environment al"/><label
            for="environmen tal">Environmen tal</label><br/>
            <div style="width:10 px"></div><input type="checkbox"
            name="subscript ion" value="politica l" id="political"/><label
            for="political" >Political</label><br/>
            <div style="width:10 px"></div><input type="checkbox"
            name="subscript ion" value="social" id="social"/><label
            for="social">So cial</label><br/>
            <br><input type="submit" value="Submit" style="font-size:10px"/>
            </div>
            <input type="hidden" name="add_subsc ription" value="1"/>
            </form>

            Once the user ticks the boxes that apply to them, they click the submit
            button and I want to go through the boxes. I am using the following type of
            statement:

            if('".$subscrip tion[0]."'==0){
            $query = "INSERT INTO inj_subscriptio n VALUES ('$user' , 'economic' ,
            NULL);";
            $result = mysql_query($qu ery) or die('Error during subscription process,
            please refresh the page and try again');
            if($result){
            echo '<p style="color:re d">Subscript ion Successful</p>';
            }
            }

            The clause that I use to get into the loop is:

            if($_GET['add_subscripti on']==1){

            The problem is that the code I have written checks whether the boxes are
            ticked when the page loads up, rather than after the user clicks the submit.
            I know that loop worked before as I was using radio buttons rsther than
            checkboxes, is this the wrong assumption to make?

            Any help would be much appreciated.

            Paul


            Comment

            Working...