Page 1 of 3 123 LastLast
Results 1 to 10 of 27

Thread: Conditional Display of Form Fields

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    602
    Thanks
    266
    Thanked 13 Times in 13 Posts

    Default Conditional Display of Form Fields

    I am creating a data entry form with a drop-down list of locations from which to choose. I have a checkbox next to "Please add my location to the list," in case the desired location is not on the list.

    I would like the fields for adding the new location to be visible only when someone checks the box. So far nothing happens when they check the box - the fields do not show up. What am I missing? Thanks, e

    Code:
    <?php if(isset($_POST['addnewloc'])) { ?>
        <div id="newloc">
          <p><label for="location">New Location:</label><input type="text" name="location" size="60"></p>
          <p><label for="address">Address:</label><input type="text" name="address" size="40"></p>
          <p><label for="city">City:</label><input type="text" name="city" size="40"></p>
          <p><label for="zip">Zip Code:</label><input type="text" name="zip" size="12"></p>
       </div>
    <?php } ?>

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    What's the identity of your checkbox? Try to give it an id of trig (as of now), and have this script:
    Code:
    <script type="text/javascript">
    window.onload=function()
    {
    var loc=document.getElementById('newloc'), // assign the element having an ID of newloc to loc var.
    trig=document.getElementById('trig'); // get the ID of checkbox
    loc.style.display='none';
    trig.onclick=function()
    {document.getElementById('newloc').style.display=(this.checked!=true)?'none':'';}}
    </script>
    If you've got an id of the checkbox already, then replace the highlighted with what you have.

    See if it helps.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. The Following User Says Thank You to rangana For This Useful Post:

    kuau (07-23-2008)

  4. #3
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    602
    Thanks
    266
    Thanked 13 Times in 13 Posts

    Default

    Do I replace every instance of trig? This is what I tried, but I'm not sure where to put it. I got the message "Error on page." Thanks so much.

    Code:
    <p>Please add my location to the list: <input type="checkbox" id="addnewloc" name="addnewloc" value="Y" <?php echo ( ! empty ( $addnewloc ) ? "checked" : "" ); ?> ">
    <script type="text/javascript">
    window.onload=function()
    {
    var loc=document.getElementById('newloc'), // assign the element having an ID of newloc to loc var.
    addnewloc=document.getElementById('addnewloc'); // get the ID of checkbox
    loc.style.display='none';
    addnewloc.onclick=function()
    {document.getElementById('newloc').style.display=(this.checked!=true)?'none':'';}}
    </script>      
          
    <?php if(isset($_POST['addnewloc'])) { ?>
    
      <div id="newloc">
          <p><label for="location">New Location:</label><input type="text" name="location" size="60"></p>
          <p><label for="address">Address:</label><input type="text" name="address" size="40"></p>
          <p><label for="city">City:</label><input type="text" name="city" size="40"></p>
          <p><label for="zip">Zip Code:</label><input type="text" name="zip" size="12"></p>
       </div>
    <?php } ?>

  5. #4
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    I can't reproduce the problem on my end, but maybe because I've used the onload event and you might have used it already.

    With that said, have a try by having the highlighted instead of the script I've given:
    Code:
    <input type="checkbox" id="addnewloc" name="addnewloc" value="Y" <?php echo ( ! empty ( $addnewloc ) ? "checked" : "" ); ?> " onclick="document.getElementById('newloc').style.display=(this.checked!=true)?'none':'';">
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  6. The Following User Says Thank You to rangana For This Useful Post:

    kuau (07-23-2008)

  7. #5
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    602
    Thanks
    266
    Thanked 13 Times in 13 Posts

    Default

    I don't have a <body onload=??>. Do I need one?

  8. #6
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    No. The addition on the input element will (might) work.

    Does it throws an error when added?
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  9. The Following User Says Thank You to rangana For This Useful Post:

    kuau (07-23-2008)

  10. #7
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    602
    Thanks
    266
    Thanked 13 Times in 13 Posts

    Default

    OK, I tried that and got the error "Object expected on line 393." There are only 244 lines in the code.

  11. #8
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Could you show us your complete modification instead.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  12. The Following User Says Thank You to rangana For This Useful Post:

    kuau (07-23-2008)

  13. #9
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    602
    Thanks
    266
    Thanked 13 Times in 13 Posts

    Default

    I have commented out a section of garbage code in the file while I am working on it -- could this be conflicting? Thanks for helping me.
    Last edited by kuau; 07-27-2008 at 05:39 AM. Reason: removed dead link

  14. #10
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Thanks for the link. Together with the link is a vivid reason of the error.

    You don't have an element having an id of newloc.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  15. The Following User Says Thank You to rangana For This Useful Post:

    kuau (07-23-2008)

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
  •