Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Oct 2011
    Posts
    237
    Thanks
    11
    Thanked 5 Times in 5 Posts

    how do i only show a input text box if select option is present

    Hi Guys i am a complete novice at ajax or javascript.

    I have been studying php for a while now and i know that, i am now starting php oop and that going well but i have never done anything like this.

    Anway i am creating a booking system which at the end of the process asks who supplied your voucher which they then select a supplier.

    The issue i have is my client wants it so if groupon is selected then an input text box should appear so that the user can input a security code from groupon.

    So basically it is

    Code:
    <select name="supplier" id="supplier">
       <option value="">please select supplier</option>
       <option value="Groupon">Groupon</option>
       <option value="KGB Deals">KGB Deals</option>
       <option value="Other">Other</option>
    </select>
    So if value="Groupon" then
    Code:
    <input type="text" name="security" id="security" />
    should appear next to it.

    Could someone please help me as the page cant refresh as it will lose its data which would have taken the user about 10 minutes to fill out

    Thanks in advance

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Start off with display:none; style on "security."

    Then you need an onchange event handler for "supplier" to check the new value given in that dropdown. If the value is "Groupon" then give "security" block display style.

    Very roughly like this:
    Code:
    <select name="supplier" id="supplier" onchange="if(this.value=='Groupon'){document.getElementById('security').style.display='block';}else{document.getElementById('security').style.display='none';}">
    	<option value="">please select supplier</option>
    	<option value="Groupon">Groupon</option>
    	<option value="KGB Deals">KGB Deals</option>
    	<option value="Other">Other</option>
    </select>
    
    <input type="text" name="security" id="security" style="display:none;" />
    You should get the inline stuff pulled out into external commands and styles of course, but this illustrates the most basic approach to this behavior.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    devinmaking (12-16-2011)

  • #3
    Regular Coder
    Join Date
    Oct 2011
    Posts
    237
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by Rowsdower! View Post
    Start off with display:none; style on "security."

    Then you need an onchange event handler for "supplier" to check the new value given in that dropdown. If the value is "Groupon" then give "security" block display style.

    Very roughly like this:
    Code:
    <select name="supplier" id="supplier" onchange="if(this.value=='Groupon'){document.getElementById('security').style.display='block';}else{document.getElementById('security').style.display='none';}">
    	<option value="">please select supplier</option>
    	<option value="Groupon">Groupon</option>
    	<option value="KGB Deals">KGB Deals</option>
    	<option value="Other">Other</option>
    </select>
    
    <input type="text" name="security" id="security" style="display:none;" />
    You should get the inline stuff pulled out into external commands and styles of course, but this illustrates the most basic approach to this behavior.
    Thank you for the help, worked a treat


  •  

    Posting Permissions

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