multiple checkbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mudgilgaurav
    New Member
    • Oct 2006
    • 7

    multiple checkbox

    Hi

    I have multiple checkboxes on my page and i want to access only the selected checkbox on the next page can any one give me the idea how to do it

    Thnx in advance

    Bye
  • bishwadeep
    New Member
    • Nov 2006
    • 12

    #2
    Please see the following example. Hope it will help you
    Code:
    <body>
    <form action="checkbox.php" method="post">
      <input type="checkbox" name="checkbox" value="a">
      <input type="checkbox" name="checkbox" value="b">
      <input type="checkbox" name="checkbox" value="c">
      <input type="checkbox" name="checkbox" value="d">
      <br>
      <br>
      <input type="submit" name="Submit" value="Submit">
    </form>
    <?
    	if(isset($_POST['Submit']))
    	{
    		echo $_POST['checkbox'];
    	}
    ?>
    </body>
    Last edited by ronverdonk; Dec 11 '06, 12:17 PM. Reason: code tags!!!

    Comment

    • mudgilgaurav
      New Member
      • Oct 2006
      • 7

      #3
      Originally posted by bishwadeep
      Please see the following example. Hope it will help you
      Code:
      <body>
      <form action="checkbox.php" method="post">
        <input type="checkbox" name="checkbox" value="a">
        <input type="checkbox" name="checkbox" value="b">
        <input type="checkbox" name="checkbox" value="c">
        <input type="checkbox" name="checkbox" value="d">
        <br>
        <br>
        <input type="submit" name="Submit" value="Submit">
      </form>
      <?
      	if(isset($_POST['Submit']))
      	{
      		echo $_POST['checkbox'];
      	}
      ?>
      </body>
      HI

      Thnx for ur response
      this thing know already i asked if i select 3 checkboxes on the first page then how to access all those three selected check boxes the method u gave print only the last selected box

      if u know then plz reply

      bye

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Originally posted by mudgilgaurav
        HI

        Thnx for ur response
        this thing know already i asked if i select 3 checkboxes on the first page then how to access all those three selected check boxes the method u gave print only the last selected box

        if u know then plz reply

        bye
        This sample is incorrect! It treats the checkboxes like radio buttons because you give them all an identical name. And only one of the names can be set that way. When you want to get all the values that have been checked, you make the name field an array. So when you click checkboxes 1 and 3 (values 'a' and 'c'), you have an array that, when printed using print_r looks like:
        Code:
        Array ( 
               [0] => a 
               [1] => c 
              )
        So in your catching script you must process the array with name $_POST['checkbox']
        Here is the code:
        [php]<body>
        <form action="checkbo x.php" method="post">
        <input type="checkbox" name="checkbox[]" value="a">
        <input type="checkbox" name="checkbox[]" value="b">
        <input type="checkbox" name="checkbox[]" value="c">
        <input type="checkbox" name="checkbox[]" value="d">
        <br>
        <br>
        <input type="submit" name="Submit" value="Submit">
        </form>
        <?

        /* and in your checkbox.php you do this: */

        if(isset($_POST['Submit']))
        {
        for ($i=0; $i<count($_POST['checkbox']);$i++) {
        echo "<br />value $i = ".$_POST['checkbox'][$i];
        }
        }
        ?>
        </body>
        [/php]
        When you select boxes 2 and 4, the result of this script is then
        Code:
        value 0 = b
        value 1 = d
        Ronald :cool:

        Comment

        • crisis123
          New Member
          • Jun 2009
          • 18

          #5
          hi i have multiple checkboxes on my page and i wanna select only one at a time...i dont want to use radio buttons.

          can anyone provide some help??

          thanku

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            but radio button would be best suited for that (i.e. they allow only one to be checked in a group)

            anything beyond that requires JavaScript. (you can make it, so that you can select only one checkbox, but if the user disables JavaScript…)

            Comment

            • crisis123
              New Member
              • Jun 2009
              • 18

              #7
              thanks a lot
              well i had been askd to use the checkbox by the end user..thats why i had this problem.

              another problem is that i have used <input type="checkbox" name="yes[]" value="Y"/> for creating the checkbox
              but when i create a variable $checkbox=$_POS T['yes'] in my php script i get an error of undefined index
              i have used post method to create the form

              any suggestions?

              Comment

              • crisis123
                New Member
                • Jun 2009
                • 18

                #8
                sorry i have used $checkbox=$_POS T['yes[]'] nd still encountered that undefined index error

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  unchecked boxes (and radiobuttons) do not appear in $_POST.

                  do
                  Code:
                  var_dump($_POST);
                  to see what's present in the posted data.

                  you can also use isset() to check, if the variable/element exists.

                  Comment

                  • crisis123
                    New Member
                    • Jun 2009
                    • 18

                    #10
                    thanks a lot
                    i didnt know about the fact nd wasnt getting any info regarding it

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      you may also experiment with the filter functions like filter_has_var( ).

                      Comment

                      • crisis123
                        New Member
                        • Jun 2009
                        • 18

                        #12
                        can i use $_POST for upload section?

                        i didnt get any error on using it but is there a better way?

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          Originally posted by crisis123
                          can i use $_POST for upload section?
                          do you mean file upload? no, you need to use $_FILES for that.

                          Comment

                          • crisis123
                            New Member
                            • Jun 2009
                            • 18

                            #14
                            thanks a lot
                            i have just started working on php
                            this is my 1st assignment

                            Comment

                            • Dormilich
                              Recognized Expert Expert
                              • Aug 2008
                              • 8694

                              #15
                              a very valuable resource

                              Comment

                              Working...