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

    Join Date
    Sep 2005
    Rep Power

    Toggling Display with Checkbox


    Totally frustrated! Have checkbox and need to toggle display with it. Basically have the following code:
    <? echo $def_dis = default_display (); ?>   // get the default display html
    <input name=mycheck type=checkbox> Advanced Search
       if (mycheck=='on') { echo $adv_dis = advanced_display(); } //get the advanced display 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!

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

    Join Date
    Mar 2007
    Washington, USA
    Rep Power
    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
    Oxford, United Kingdom
    Rep Power
    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).

    <input name=mycheck type=checkbox>
    <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.
    <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:
        if ($(this).is(':checked')) {
    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