Jump to content

Recommended Posts

I have a form with a standard text box that i would like to turn into a drop down box that is populated from a mysql table.

How do I change the code of the text box so that it looks at the table and populates the list?

 

Here is the code for the text box

<input type="text" name="country" size="20" maxlength="20">

 

The mysql table has the following fields in it:

 

ccode: where ccode would be AU

Name: where name would be Australia

Link to comment
https://forums.phpfreaks.com/topic/53706-select-country-from-second-table/
Share on other sites

I prefer to do them as functions, passing the currently selected value as the argument

 

<?php
function countrySelect($current='') 
{
    $sql = "SELECT ccode, name
            FROM country
            ORDER BY name";
    $res = mysql_query($sql);
    $str = "<select name='country'>\n";
    $str .= "<option value='' >- country -</option>\n";
    while (list($cc,$name) = mysql_fetch_row($res)) {
        $sel = $cc==$current ? 'selected' : '';
        $str .= "<option value='$cc' $sel> $name</option>\n";
    }
    $str .= "</select>\n";
    return $str;
}

/**
* to display showing Australia as the selected value
*/

echo countrySelect('AU');
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.