Results 1 to 5 of 5

Thread: If Else Input checked?

  1. #1
    Join Date
    Jul 2011
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default If Else Input checked?

    Hi,

    How would I write a php if else statement to check to see if the INPUT checkbox is clicked then Do this.

    I have a category input check box which I want to expand into a sub category once someone clicks on this check box.

    Sorry about the bad english LOL...i hope you understand what I mean.

    Thanks in advance.

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    The PHP solution assumes that you submitted a form with a checkbox. If you want live results (as soon as it's clicked, an event is triggered), you need to use JavaScript.

    PHP solution:
    PHP Code:
    <?php

    if(isset($_POST['chkbx'])) {
    echo 
    "Checkbox was set";
    }

    ?>
    <input type="checkbox" name="chkbx" value="1" />
    JavaScript solution:
    Code:
    <script type="text/javascript">
    function isChecked(obj) {
    if(obj.checked === true) {
    // Perform event.
    }
    }
    </script>
    
    <input type="checkbox" name="chkbx" value="1" onclick="isChecked(this)" />
    - Josh

  3. #3
    Join Date
    Jul 2011
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Hi thanks for the help...one question.....what if i have multiple checkbox would I have to give a unique name to the functions for each??

  4. #4
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    No. You can have as many checkboxes as you want.

    This would be perfectly valid:
    Code:
    <script type="text/javascript">
    function isChecked(obj) {
    if(obj.checked === true) {
    // Perform event.
    }
    }
    </script>
    
    <input type="checkbox" name="chkbx" value="1" onclick="isChecked(this)" />
    <input type="checkbox" name="chkbx2" value="2" onclick="isChecked(this)" />
    <input type="checkbox" name="chkbx3" value="3" onclick="isChecked(this)" />
    Unless you wanted to execute different events for each different checkbox. Then you would need to write new functions, obviously.
    - Josh

  5. #5
    Join Date
    Jul 2011
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Hi Jshor,

    I'm anaylizing your code and it looks like what it does is that when ever there's a TRUE to checkbox it "Performs event". Sorry for not making it clearer in the beginning...I don't think it will work for what i want. I have let's say 2 categories with 2 sub categories from the main ones. SO

    Category 1
    CARS
    -BMW

    Category 2
    PLANES
    -F1 Tiger

    From what I see in your code it performs 1 event when = TRUE....I need a code or a modified one from yours to do multiple events depending on which checkbox is clicked.

    Thanks alot again Jshor






    Quote Originally Posted by JShor View Post
    No. You can have as many checkboxes as you want.

    This would be perfectly valid:
    Code:
    <script type="text/javascript">
    function isChecked(obj) {
    if(obj.checked === true) {
    // Perform event.
    }
    }
    </script>
    
    <input type="checkbox" name="chkbx" value="1" onclick="isChecked(this)" />
    <input type="checkbox" name="chkbx2" value="2" onclick="isChecked(this)" />
    <input type="checkbox" name="chkbx3" value="3" onclick="isChecked(this)" />
    Unless you wanted to execute different events for each different checkbox. Then you would need to write new functions, obviously.

Bookmarks

Posting Permissions

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