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 Coder
    Join Date
    Jul 2008
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Passing Info From Html Checkbox to php

    I'm trying to create a bunch of checkboxes that pass information to a php script in code.

    When "Page1" is checked, I need a php variable $page1 to be set equal to false. The same for Page2 and Page3. When the button is clicked, a php variable $run becomes set equal to true.

    Code:
    <form name="myform" action="" method="POST">
    <div align="center"><br>
    <input type="checkbox" name="option1" value="Page1"> Page1<br>
    <input type="checkbox" name="option2" value="Page2" checked> Page2<br>
    <input type="checkbox" name="option3" value="Page3"> Page3<br>
    <br>
    </div>
    </form>
    <button type="button">Click Here</button>

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    As you've noticed, checkboxes that are unchecked do not show up at all on the back end. This is a "feature" of the HTTP request and is not unique to PHP. ASP, Cold Fusion, and all other HTTP parsers have to deal with it.

    There is a way to combat it though. Create input elements, type="hidden", using the same name as the checkbox. This hidden element must be placed before the checkbox. Set the value of this hidden element to "false", or "0", or "off", or whatever.

    Set the value of the checkbox itself to "true", "1", or "on".

    With this hidden element, the HTTP request now brings its value across to PHP, unless the checkbox is checked, in which case the checkbox's value is brought across to PHP.

    Code:
    <form name="myform" action="" method="POST">
    <div align="center"><br>
    <input type="hidden" name="option1" value="false">
    <input type="hidden" name="option2" value="false">
    <input type="hidden" name="option3" value="false">
    <input type="checkbox" name="option1" value="Page1"> Page1<br>
    <input type="checkbox" name="option2" value="Page2" checked> Page2<br>
    <input type="checkbox" name="option3" value="Page3"> Page3<br>
    <br>
    </div>
    </form>
    <button type="button">Click Here</button>

  • Users who have thanked Fumigator for this post:

    walker6o9 (12-05-2008)

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,511
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    There's more than one way to do this, but since you're using variables
    instead of an array (example, you're using $page1, $page2, $page3) ...

    The form:
    <form name="myform" action="test.php" method="POST">
    <div align="center"><br>
    <input type="checkbox" name="option[]" value="1"> Page1<br>
    <input type="checkbox" name="option[]" value="2" checked> Page2<br>
    <input type="checkbox" name="option[]" value="3"> Page3<br>
    <br>
    <input type="submit" name="submit" value="Click Here">
    </div>
    </form>


    The PHP script:
    PHP Code:
    <?php

    $page1
    =0;
    $page2=0;
    $page3=0;

    if(
    $_POST['option']){
       
    $opts $_POST['option'];
       foreach (
    $opts as $o) {
          if(
    $o == 1){$page1=1;}
          if(
    $o == 2){$page2=1;}
          if(
    $o == 3){$page3=1;}
       }
    }
    echo 
    $page1.$page2.$page3

    ?>

  • Users who have thanked mlseim for this post:

    walker6o9 (12-05-2008)

  • #4
    New Coder
    Join Date
    Jul 2008
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts
    So, I tried re-working your code, to set a variable "run" that is normally equal to zero, but gets set equal to one when the button is clicked. The problem is that run is always set to one.

    Code:
    <form name="myform" action="starter_pdfmerge.php" method="POST">
    <div align="center"><br>
    <input type="checkbox" name="option[]" value="1"> Page1<br>
    <input type="checkbox" name="option[]" value="2" checked> Page2<br>
    <input type="checkbox" name="option[]" value="3"> Page3<br>
    <br>
    <input type="submit" name="option[]" value="Click Here"> Click Here <br>
    </div>
    </form>
    
    
    <?php
    
    
    $page1=0;
    $page2=0;
    $page3=0;
    $run = 0;
    
    if($_POST['option']){
       $opts = $_POST['option'];
       foreach ($opts as $o) {
          if($o == 1){$page1=1;}
          if($o == 2){$page2=1;}
          if($o == 3){$page3=1;}
          if($o == "Click Here"){$run=1;}
       }
    }
    echo $page1.$page2.$page3.$run;
    Last edited by walker6o9; 12-05-2008 at 08:44 PM.


  •  

    Posting Permissions

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