SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict Tizer's Avatar
    Join Date
    May 2002
    Location
    Beach
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Select Drop Down with 'Other' Textfield Option?

    So I have a select drop down in a database input form which works good
    Code:
    <select  name="howheard"  id="howheard">
    		<option value="">select</option>
    	<?php
    	$qry_heard="select * from bbe_howheard";
    	$res_heard=mysql_query($qry_heard);
    	while($rows=mysql_fetch_array($res_heard))
    	{
    		$heard_id=$rows['hhid'];
    		$heard_code=$rows['howheard'];
    	?>
    		<option value="<?php echo $heard_code;?>" <?php if($heard_code==$ary_sel['HEARD']) echo 'selected="selected"';?> ><?php echo $heard_code;?></option>;
    	<?php
    	}
    	?>
    	</select>
    I have just been asked to add an "Other" option to it with a text field next to it where they can enter the "Other" option.

    This would be entered into the same field as the select drop down - but I have no idea how to do that.

    Can anyone point me to any tutorials on this - I really haven't got a clue on this one - I don't even know what to search for

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    The 'other' option would require a completely different form element, so you would need to add some logic on the script which processes your form.

    You would check to see if the 'other' option has a value, if so, use that and throw away what ever is contained with the drop-down and process accordingly.

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Addict Tizer's Avatar
    Join Date
    May 2002
    Location
    Beach
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks SilverBullet - I know what you are saying to do... but I'm not sure if I have the php knowledge to do it at the mo.

    Do you know of a tutorial, or can you tell me what to use when searching for a solution... please

  4. #4
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just take a stab at it.

    Code:
    1. Create the new text box in the form code block
    2. Check if the text box has something in it
      1. If it's not empty use it's value
      2. If not use the select box value
    If you need further help, just post the code your using and we'll throw you some pointers.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nothing such difficult though:

    - Just add a text field next to selection field always visible until you don't want to hide it in other case. (If you want to do so, then you will have some JavaScript for this)
    PHP Code:
    <select  name="howheard"  id="howheard">
        <option value="">select</option>
        <?php
        $res_heard 
    mysql_query("SELECT * FROM bbe_howheard") or die(mysql_error());
        while(
    $rows mysql_fetch_array($res_heard)){
            
    $heard_id $rows['hhid'];
            
    $heard_code $rows['howheard'];
            
    ?>
            <option value="<?php echo $heard_code;?><?php if($heard_code==$ary_sel['HEARD']) echo 'selected="selected"';?> ><?php echo $heard_code;?></option>;
            <?php
        
    }
        
    ?>
        <option value="Other">Other</option>
    </select>
    Other : <input type="text" name="other_howheard" id="other_howheard" />
    And when you post, then do something like this:
    PHP Code:
    $howheard $_POST['howheard'];
    if(
    'Other' == $howheard){
        
    $howheard $_POST['other_howheard'];

    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Here's a quick example, hack away at it and feel free to post your solution.

    PHP Code:
    <?php
    if(isset($_POST))
    {
        if(isset(
    $_POST['source']['other']) && !empty($_POST['source']['other']))
        {
            
    $sSource $_POST['source']['other'];
        }
        else
        {
            
    $sSource $_POST['source']['multiple'];
        }
        echo 
    $sSource#this contains the source now.
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title></title>
        </head>
        <body>
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                <select name="source[multiple]">
                    <option>Magazine</option>
                    <option>Newspaper</option>
                    <option>Search Engine</option>
                </select>
                <input type="text" name="source[other]" />
                <input type="submit" />
            </form>
        </body>
    </html>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And I would go like this with simple JavaScript to toggle the input field:
    PHP Code:
    <?php
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        
    $howheard $_POST['howheard'];
        if(
    'Other' == $howheard){
            
    $howheard $_POST['other_howheard'];
        }
        echo 
    $howheard;
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title></title>
            <script type="text/javascript">
            function ShowHideOther(source){
                if('Other' == source){
                    document.getElementById('spnOthers').style.display = '';
                }
                else{
                    document.getElementById('spnOthers').style.display = 'none';
                }
            }
            </script>
        </head>
        <body>
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                <select  name="howheard"  id="howheard" onChange="ShowHideOther(this.value);">
                    <option value="">select</option>
                    <option value="Magazine">Magazine</option>
                    <option value="Newspaper">Newspaper</option>
                    <option value="Search Engine">Search Engine</option>
                    <option value="Other">Other</option>
                </select>
                <span id="spnOthers" style="display:none;">Other : <input type="text" name="other_howheard" id="other_howheard" /></span>
                <input type="submit" />
            </form>
        </body>
    </html>
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  8. #8
    SitePoint Addict Tizer's Avatar
    Join Date
    May 2002
    Location
    Beach
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool - I've figured it out (with your help of course).

    I tried rajugs method first - but couldn't get it to work with my existing code.

    So I tried SilverBullet's and this is what I have (which is pretty similar):

    PHP Code:
    <?php
    if($_POST['continue'])
    {
    //$howheard = $_POST['howheard'];
        
    if(isset($_POST['source']['other']) && !empty($_POST['source']['other']))
        {
            
    $howheard $_POST['source']['other'];
        }
        else
        {
            
    $howheard $_POST['source']['multiple'];
        }
        
    //echo $sSource; #this contains the source now.[/CODE]
    }
    ?>
    And then renamed my form elements like this:
    Code:
    <select  name="source[multiple]"  id="howheard">
    <option value="">select</option>
    	<?php
    	$qry_heard="select * from bbe_howheard";
    	$res_heard=mysql_query($qry_heard);
    	while($rows=mysql_fetch_array($res_heard))
    	{
    		$heard_id=$rows['hhid'];
    		$heard_code=$rows['howheard'];
    	?>
    <option value="<?php echo $heard_code;?>" <?php if($heard_code==$ary_sel['HEARD']) echo 'selected="selected"';?> ><?php echo $heard_code;?></option>;
    	<?php
    	}
    	?>
    	</select>
    
    If Other, please specify: <input type="text" name="source[other]" />
    So, my understanding is
    PHP Code:
    if(isset($_POST['source']['other']) && !empty($_POST['source']['other'])) 
    is checking to see if there is anything in the text field "source[other]"

    If there is, then
    Code:
    $howheard = $_POST['source']['other'];
    outputs this if not then the value at
    Code:
    $howheard = $_POST['source']['multiple'];
    outputs the value of the select instead

    I'm confused as to what is going on here though when using:
    Code:
    $_POST['source']['other']; $_POST['source']['multpile'];
    Is this a way with dealing with multiple variables by combining them?


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
  •