#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    126
    Rep Power
    10

    Toggling Display with Checkbox


    All,

    Totally frustrated! Have checkbox and need to toggle display with it. Basically have the following code:
    Code:
    <html>
    <? echo $def_dis = default_display (); ?>   // get the default display html
    <input name=mycheck type=checkbox> Advanced Search
    <br>
    <?
       if (mycheck=='on') { echo $adv_dis = advanced_display(); } //get the advanced display html
    ?>
    </html>
    Problem is when it turns on it will not turn off and when the "Search" button on the page is clicked currently throws errors, so really frustrated.

    Additional frustration, is I can find only 2 examples of this and when entering "php toggle display with checkbox" I only get $##@$%%^ JavaScript, instead of PHP. I totally hate search engines that "GUESS" what I'm searching for. When I say "PHP" I want to see only PHP solutions, not friggin jAVAsCRIPT!

    Because of this SE bullcrap, having to take time to write this up and hate wasting precious coding time.

    All help with this appreciated!

    OMR
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,119
    Rep Power
    9398
    It's not possible to change the HTML of the page with PHP.

    Output the advanced stuff regardless of the checkbox, but make it hidden (eg, display:none) if it shouldn't be shown. The checkbox then controls whether it's visible.
    It's still on the page, you just can't see it.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Oxford, United Kingdom
    Posts
    40
    Rep Power
    2
    I'll make this quite simple.

    A HTML Checkbox is something of the front-end world. The "display" CSS property is also something of the front-end world.

    PHP is a back-end scripting language. It doesn't handle anything of the front-end world. That's why JavaScript exists - to handle all your front-end excitement.

    So when you want to alter the CSS "display" property value, you have to do it through the front-end (use JS). And when you want to use an event listener to listen for when the user checks/un-checks the Checkbox, you also have to do that in the front-end, too.

    I'm assuming you don't want to use JS because you don't know how to. It's actually really easy (with jQuery).

    Change
    Code:
    <input name=mycheck type=checkbox>
    To
    Code:
    <input name=mycheck type=checkbox id="checkBox1">
    Then have this script-include in your <head> section (of your doc). What this will do is include the jQuery library, which will make your front-end life much easier.
    Code:
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    Now, just below your checkbox, you can have your event listener and handler:
    Code:
    $('#checkBox1').mousedown(function() 
    {
        if ($(this).is(':checked')) {
        {
              $('#elementToHide').hide();
              return;
        }
    }});
    So the code above waits for the user to click on the checkbox. It then checks if the checkbox is checked - if it is, it will hide the element with the ID "elementToHide" You can change that ID to whatever the ID of the element you want to hide is.

    But if you really want PHP to not even output the data onto the page, then I guess you could just do a refresh (JS redirect) with a simple option value in the URL query ($_GET), but that will still involve JS (for the redirect).

IMN logo majestic logo threadwatch logo seochat tools logo