Jump to content

Remembering combobox selection.


cjkeane

Recommended Posts

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!';
}
}

Link to comment
Share on other sites

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.