cjkeane Posted February 24, 2011 Share Posted February 24, 2011 Hi Everyone. I'm using the following code to populate a combobox with salutations. The List is populated and the initial value is 'select a salutation'. When the submit button is clicked, the selection is saved in the database. my issue is that when i open the page based on the id number, the list does not remember my choice. it just lists all the salutations in the combobox. How can i change this code to remember the value selected? thanks. I am saving the value of Salutation_1 from the combobox into records.Salutation_1. <?php $result=mysql_query("select Salutation from salutations"); $options=""; while ($row=mysql_fetch_array($result)) { $categoryname=$row["Salutation"]; $options.="<OPTION VALUE=\"$categoryname\">".$categoryname.'</option>'; } ?> <select name="Salutation_1"> <option >< select salutation > <?php echo $options ?></option> Quote Link to comment https://forums.phpfreaks.com/topic/228743-remembering-combobox-selection/ Share on other sites More sharing options...
Psycho Posted February 24, 2011 Share Posted February 24, 2011 You first need to get the saved value from the database. Then simply add a "switch" to detemine if an option should be selected or not and add selected="selected" to the option. In this example code the variable $selectedValue is assumed to be the selected value you extracted from the DB <?php $result = mysql_query("SELECT Salutation FROM salutations"); $options = ''; while ($row = mysql_fetch_assoc($result)) { $selected = ($row['Salutation']==$selectedValue) ? ' selected="selected"' : ''; $options .= "<option value=\"{$row['Salutation']}\"{$selected}>{$row['Salutation']}</option>\n"; } ?> <select name="Salutation_1"> <option>< select salutation ></option> <?php echo $options ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/228743-remembering-combobox-selection/#findComment-1179313 Share on other sites More sharing options...
cjkeane Posted February 24, 2011 Author Share Posted February 24, 2011 I gave that a try, but the still doesn't remember the selection. It still defaults back to < select a salutation >. The salutations are coming from table 'salutations' but the value is being saved in table 'records'. Somehow I need to check records.salutation_1 to see if it was selected. I hope that makes more sense. Quote Link to comment https://forums.phpfreaks.com/topic/228743-remembering-combobox-selection/#findComment-1179320 Share on other sites More sharing options...
Psycho Posted February 25, 2011 Share Posted February 25, 2011 The code you posted only contained the information needed to generate the select list. As I stated you need to acquire the selected value in order to auto-select it. As I also stated the $selectedValue variable is assumed to contain the, well, selected value. Since you did not provide any information on how to obtain that value I left it to you to do. If the selected value is in the records table, then you need to query that table to get it and use it in the code I provided. Quote Link to comment https://forums.phpfreaks.com/topic/228743-remembering-combobox-selection/#findComment-1179570 Share on other sites More sharing options...
cjkeane Posted February 25, 2011 Author Share Posted February 25, 2011 Sorry, I thought providing the information above was enough to get the idea of what I needed to accomplish. I had queried the db with the following code so I thought it should have found the selected value of the combobox Salutation_1: // get the 'IDNumber' value from the URL (if it exists), making sure that it is valid if (isset($_GET['IDNumber'])) { // query db $IDNumber = $_GET['IDNumber']; $result = mysql_query("SELECT * FROM records WHERE IDNumber='$IDNumber'") or die(mysql_error()); $row = mysql_fetch_array($result); // check that the 'IDNumber' matches up with a row in the databse if($row) { // get data from db $Salutation_1 = $row['Salutation_1']; $DateOfBirth = $row['DateOfBirth']; $NameFirst_1 = $row['NameFirst_1']; $NameLast_1 = $row['NameLast_1']; $NameMaiden_1 = $row['NameMaiden_1']; $ContactTitle_1 = $row['ContactTitle_1']; // show form renderForm($IDNumber, $Salutation_1, $DateOfBirth, $NameFirst_1, $NameLast_1, $NameMaiden_1, $ContactTitle_1, ''); } else // if no match, display result { echo "No results!"; } } else // if the 'IDNumber' in the URL isn't valid, or if there is no 'IDNumber' value, display an error { echo 'Error!'; } } Quote Link to comment https://forums.phpfreaks.com/topic/228743-remembering-combobox-selection/#findComment-1179774 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.