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 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts

    how to display combo box value into a textbox?

    Hi there can any one help me..? if i select some name in combobox it will display the name in textbox..



    PHP Code:

    <?php
    mysql_connect
    ("localhost""root""") or die('MySQL Connection Error:'.mysql_error());
        
    mysql_select_db("myDB") or die('MySQL Error: Cannot select table');



    echo 
    "<form name='ge' action='' method='post'>";
        echo 
    "<tr>Select Customer:<br>";
    echo 
    "<select name='selcus'>";
    echo 
    "<option value='0'>Select</option>";
    $sql="SELECT * FROM customer order by cname";
    $query=mysql_query($sql);

    while (
    $list mysql_fetch_array($query)) {

                        
                    echo 
    "<option selsected value='$list[cname]'>$list[cname]</option>"."";

    echo 
    "</select>";

        
    ?>
        
        
        <input type='text' name='cname' value="<?php echo "$cname"?>" readonly/>

    </body>
    </html>

    i don't know what to do.. can anyone help me with this? BTW im required to use php

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Why would you need to display the name in both the selection menu and a text input?
    Using just PHP would be more work than its worth IMO. I'll just change a couple of things here:
    PHP Code:
    <?php
    mysql_connect
    ("localhost""root""") or die('MySQL Connection Error:'.mysql_error());
        
    mysql_select_db("myDB") or die('MySQL Error: Cannot select table');



    echo 
    "<form name='ge' action='' method='post'>";
        echo 
    "<tr>Select Customer:<br>";
    echo 
    "<select name='selcus'>";
    echo 
    '<optgroup label="Select">';
    $sql="SELECT * FROM customer order by cname";
    $query=mysql_query($sql);

    while (
    $list mysql_fetch_array($query))
    {
        
    $selected '';
        if (isset(
    $_POST['selcus']) && strcmp($list['cname'], $_POST['selcus']) == 0// Notice that this is selcus, not cname
        
    {
            
    $selected ' selected="selected"';
        }
        
    printf('<option value="%s"%s>%s</option>' PHP_EOL$list['cname'], $selected$list['cname']);
    }

    echo 
    '</optgroup>';
    echo 
    '</select>';

    ?>
    <input type='text' name='cname' value="<?php echo htmlspecialchars(@$_POST['selcus']); ?>" readonly="readonly" /> <!-- The @ is the lazy way to do this, and should actually be avoided -->
    <input type="submit" />
    </form>
    You MUST first submit the form before you can draw the value that was selected. There is no way around this using just PHP.
    Last edited by Fou-Lu; 08-15-2009 at 12:25 AM. Reason: Added isset check for select.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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