How to show a hidden text-box when a specific option is selected.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ddtpmyra
    Contributor
    • Jun 2008
    • 333

    How to show a hidden text-box when a specific option is selected.

    This is just a follow-up question above, this time i wanted to have a dependend drop down that when the users selected 'others' for instance another texbox will show-up right next to my drop down. How can I do it from my code or should I place the condition if ever?

    thanks,
    DM

    Code:
    echo '<tr><td><b>Lead Generated By:</b></td>'; 
    $res=mysql_query("select lead  from tblgenerated"); 
    if(mysql_num_rows($res)==0){ 
    echo "there is no data in table.."; 
    } else { 
    
    	echo '	<td width="100%"><select name="lead" id="lead" value=\"$lead\">'; 
    					for($i=0;$i<mysql_num_rows($res);$i++) { 
    					$row=mysql_fetch_assoc($res); 
    					echo"<option value=\"$row[lead]\" "; 
    					if($lead==$row[lead]) 
    					echo "selected"; 
    					echo ">$row[lead]</option>"; 	
    	} 
    	echo "</select><br></tr></td>"; 
    	}
    Last edited by Atli; Feb 11 '09, 06:54 PM. Reason: Question moved into it's own thread.
  • hoopy
    New Member
    • Feb 2009
    • 88

    #2
    This sounds like you need something client side, i.e Javascript or AJAX. Add an "on change" event in the select box which checks if the value is "0" or something and if so display a hidden input box next to the drop down using CSS display; ? I dont think it should be something which needs a PHP refresh. Maybe I am misunderstandin g your question though.

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Yep, again, hoopy is spot on :)

      The *best* way to dynamically change minor details on your page, like enabling elements, is to use JavaScript.
      (* Best in like 95% of cases, as not all browsers will have JavaScript enabled)

      Simply create a <select> tag, and have the onchange event of that tag enable the box if the value of the selected option is set to a predefined value.

      For example, you could put this into the <head></head> tags:
      [code=javascript]
      <script type="text/javascript">
      function ToogleTextbox(p Value) {
      var textBox = document.getEle mentById('Other Box');
      if(pValue) {
      textBox.style.d isplay = "block";
      }
      else {
      textBox.style.d isplay = "none";
      }
      }
      </script>
      [/code]
      This function would simply find the <input> with the id "OtherBox", and then uses the display style to either show it or hide it.

      And then do something like this for your <select> tag:
      [code=html]
      <select name="MySelect" onchange="javas cript: ToogleTextbox(t his.value == 'null');">
      <option value="null">Ot her</option>
      </select>
      <input type="text" name="OtherBox" id="OtherBox" style="display: none;" />
      [/code]
      Which basically tells your <select> element to call the ToogleTextbox function, and send a boolean value, indicating whether the <option> with the value "null" is selected.

      Comment

      • xaxis
        New Member
        • Feb 2009
        • 15

        #4
        If you want multi-level drop down menus and a high degree of cross browser support, you COULD use nothing but CSS. Using something like:

        Code:
        div#hiddenMenu {
            position:absolute;
            // positioned where you would like the div with the id 'hiddenMenu' to appear
            visibility:hidden;
            width:200px;
            height:200px;
            border:1px solid #000;
        }
        
        span#menuItem:hover > #hiddenMenu {
            visibility:visible;
        }
        I won't go into more detail than that for this is the javascript forum. Just thought I'd add to the many possibilities.
        Last edited by acoder; Feb 13 '09, 11:25 AM. Reason: Please use [code] tags

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          Code:
              <select>
                  <option onclick="if (this.selected) document.getElementById('others').style.display='block';">others</option>
                  <option selected="selected" onclick="if (this.selected) document.getElementById('others').style.display='none';">HHH</option>
              </select>
              <input type="text" style="display:none;" id="others"></input>

          Comment

          • ddtpmyra
            Contributor
            • Jun 2008
            • 333

            #6
            Hello again,

            This is just a follow-up question and somewhat similar with same problem I had. This time I have javascript that has option to hide and show the button. But I dont know how to insert the sql query on the event action.

            My forms is like this:
            Code:
            echo'<input id="showbutton" type="button" value="' . translate ( "Show" )
                 . '" onclick="showComments();" />';
            My javascript is like this:
            Code:
            <script type="text/javascript">
            function showComments()
            {
            alert("I want to show the information from mysql how?!");
            }
            </script>
            Question:
            Where can i put the query select contidtion on javascript event? Please advice.

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Use PHP to get the MySQL data and generate a string. Then use PHP code to generate the JavaScript code.

              Comment

              • ddtpmyra
                Contributor
                • Jun 2008
                • 333

                #8
                Originally posted by acoder
                Use PHP to get the MySQL data and generate a string. Then use PHP code to generate the JavaScript code.
                Hi Acecoder,

                can you insert php inside the msgbox or alertbox?
                Code:
                <script type="text/javascript"> 
                function showComments() 
                { 
                alert("I want to show the information from mysql how?!"); 
                } 
                </script>
                im not good in javascript so i need little more help and specific what to do. Or are there option where you can use to show texbox with information instead of the alert box?

                thanks!

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Originally posted by ddtpmyra
                  can you insert php inside the msgbox or alertbox?
                  Yes, with something like:
                  Code:
                  function showComments() 
                  { 
                  alert("Information from mysql: <?php echo $str; ?>"); 
                  }
                  assuming it's escaped properly to avoid string problems.

                  Originally posted by ddtpmyra
                  are there option where you can use to show texbox with information instead of the alert box?
                  Yes, that would actually be better. Use a container element, e.g. div and have it hidden initially (but with the information already generated from PHP), and then in showComments show it using elem.style.disp lay = "block".

                  Comment

                  Working...