gavenf Posted May 31, 2007 Share Posted May 31, 2007 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 More sharing options...
Barand Posted May 31, 2007 Share Posted May 31, 2007 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'); ?> Link to comment https://forums.phpfreaks.com/topic/53706-select-country-from-second-table/#findComment-265820 Share on other sites More sharing options...
gavenf Posted May 31, 2007 Author Share Posted May 31, 2007 Do I put the full code into the spot where the text box is meant to go? Am I supposed to leave the text box there or will this create one? Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/53706-select-country-from-second-table/#findComment-265909 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.