PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hi,
I’m trying to return a value to a ‘select box’ in an ‘Update Form’ that has been previously stored in a field in my MSQL database. The bit of code I’m using is this:
mysql_select_db($database_imtdatabase, $imtdatabase);
$query_rstSearchLocations = "SELECT * FROM locations ORDER BY Counties_IDPK ASC";
$rstSearchLocations = mysql_query($query_rstSearchLocations, $imtdatabase) or die(mysql_error());
$totalRows_rstSearchLocations = mysql_num_rows($rstSearchLocations);
<?php $strSavedValue = $row_getUpdateDetails['LocationList'];
echo "<select name='LocationList' id='LocationList'>\n";
while($dbRow = mysql_fetch_array($rstSearchLocations)){
echo "<option value='" . $dbRow["Counties_IDPK"]."'>";
if ($dbRow["Counties_IDPK"] == $strSavedValue){
echo 'Previous Selected'; // to be removed
echo $dbRow["Counties_Name"]."</option>\n";
} echo "</select>\n";
}?>
Basically the ‘Form’ that initially passes this information is dynamically populated with a list of locations such as:
Cork town
Cork
Cork city
What I want to do is have the data that has been originally sent to the database returned to my Update Form with the specific selected value displayed in the select box but at the same time once the accompanying dropdown box is clicked will still reveal all the other data.
At the moment I’m getting this returned to the browser using the code above: http://ahamay.com/select.htm I think that my problem is to do with the use of ‘;’ etc but I’m really only at this PHP stuff since Christmas and struggle with the little things!
the sample you posted only has an image of a select box....that I could see.
one thing I see in your code that might be killing it is you have your closing </select> inside your while loop which will print it out for every iteration of the loop.
<select name="myselect">
<?while($row = mysql_fetch_assoc($result)){ ?>
<option value="<? echo $result['whatever'];?>"><? echo $result['whatevershown'];?></option>
<? } // end your while loop before you close your select ?>
</select>
if you can post a link to the correct page that'll help us better help you...
Thanks very much for your help on this, it is most appreciated.
I have tried to set up a test. You can go to this page http://www.irishmusicteachers.ie/update_test_form.php? And make a selection and you will be sent to another page where you should see the same selection that has just been previously made with the option to update it but the second page only shows the first value on the list rather that the one selected.
This is the code from the first page (update_test) and the select box is dynamically filled from the database:
if you want the previous choice to be selected, you're going to need to check if the post value is the same as the value being looped from the db, if it is, just echo "selected=\"selected\"";