Results 1 to 9 of 9

Thread: Show hide textinput depending on checkbox?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Post Show hide textinput depending on checkbox?

    Can php do this?

    So I have some checkboxes on my form and if it is checked I want to let the user give a small description of it as well ie. show a textinput for them to be able to write in....

    I'm thinking either when the checkbox is true it could somehow dynamically create a textinput (hmmm not sure if I've seen this...)

    OR

    I could have disabled textinputs next to the checkboxes and clicking on a checkbox will enable the textinput next to it?

    Are there any [better] methods and if not which should I go for and how? Thx

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Show hide textinput depending on checkbox?

    you need to use Javascript to do this. php is a server-side language and only outputs HTML, after the website is served to the client, PHP cannot physically do anything further to that website. Javascript is client-side and can be used to manipulate a website after it has been served to the client, which is exactly what you're looking for. A quick search on Google for something like 'disabling HTML with Javascript' should give you plenty of examples. If not, you can always check out Dynamic Drive, which has a large collection of Javascript-scripts, and I'm sure it also has what you're looking for, among many other things.

  3. #3
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Show hide textinput depending on checkbox?

    Of course you're probably going to want to disable/enable them onload depending on the default state of the checkbox, but:

    Code:
    <form action="blah.php" method="post">
        <input type="checkbox" name="checkbox" 
            onclick="this.form.inputtext.disabled = !this.checked" />
        <input type="text" name="inputtext" value="" />
    </form>
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Show hide textinput depending on checkbox?

    Hmmm I'm in a muddle with this - I want to add javascript and I could do this on a normal html page but my pages are structured like this:

    Code:
    <?php 
    require( "header.php" );
    ?>
    
    body
    
    <?php
    require( "footer.php" ); 
    ?>
    So how can I add the relevant js code and still have it relevant to individual pages?

  5. #5
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Show hide textinput depending on checkbox?

    You would add it wherever you are generating the check boxes and and text inputs? I'm not sure what you're confusion is. How are you generating these check boxes?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Show hide textinput depending on checkbox?

    I'm doing things like this:

    Code:
    echo "<input name='skills[]' type='checkbox' value='$skillid'>$skillname: <textarea rows='3' cols='20' name=txt$skillname></textarea><br />";
    
    $i++;
    }
    So Im generating them using the echo function.

    For some JS (not necessarily disable/enable) you have to include something in the header folder - but I'm thinking that whatever I include in the header will display on all pages cos I'm using header with the require function... I'm thinking that I don't want it to conflict with other JS code I might need or with forms on other pages... I'm actualy getting prety confused over something which is probably quite simple...lol

    (btw I like the doug pic)

  7. #7
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Show hide textinput depending on checkbox?

    For the simple code that I provided above, you do not need to include anything in the header. You do not need any <script> tags anywhere. You just put it in the onclick outright.

    As for enabling/disabling on startup, I would just do that with PHP. If you want to disable something to begin with (ie, the check box is not checked by default), add disabled="disabled" as an attribute for the textarea.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Show hide textinput depending on checkbox?

    I would not worry about your javascript in headers showing up on every single page (even if it is not needed on that page) unless you have MASSIVE javascript routines.

    And you shouldn't have to worry about conflicting with other javascript code if you just make sure to give each function a unique name, ie: "ToggleTextArea( bEnabled )".

    This will only conflict with something else if you give it the same name (declare it twice).
    My evil laugh has a squeak in it.

    kristopherwilson.com

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Show hide textinput depending on checkbox?

    Got ya

    Hey I tried what you advised above - so basically now wat I have done is set each checkbox to CHECKED on page load and when the user unchecks a box the corresponding textinput gets disabled.

    Works fine for me! Thx

    Also I note what you say about JS in PHP with the headers and require etc.. Ill test it out more later, guess I was worrying about nothing (as usual lol)

Posting Permissions

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



Click Here to Expand Forum to Full Width