Jump to content

Select Country from second table


gavenf

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');
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.