• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

Displaying stored data in <select> field in HTML form - PHP

I am writing a new front-end for an existing database application, using PHP on MySQL.

One part of the data is saved from a form that contains several drop-down lists.  These are coded in the original form using <select> tags which include lists of options in <option> tags.
Here's that form.
<form name="add_activity" method="POST" action="CPD_user_activity_save-exec.php">
<table border="0" cellpadding="4" cellspacing="4">
	<tr><td class="fieldname" valign="top">Activity</td><td class="required" valign="top" align="center"></td><td class="data"><input type="text" name="title" value="" size="60" maxlength="150"></td><td class="max_chars">- no more than 150 characters</td></tr>
	<tr>
		<td class="fieldname" valign="top">Date</td>
		<td class="required" valign="top" align="center"></td>
		<td class="data"><select name="day"><option value="">dd</option><option value="01">01</option><option value="02">02</option><option value="03">03</option><option value="04">04</option><option value="05">05</option><option value="06">06</option><option value="07">07</option><option value="08">08</option><option value="09">09</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option><option value="24">24</option><option value="25">25</option><option value="26">26</option><option value="27">27</option><option value="28">28</option><option value="29">29</option><option value="30">30</option><option value="31">31</option></select> / <select name="month" ><option value="">mm</option><option value="01">01</option><option value="02">02</option><option value="03">03</option><option value="04">04</option><option value="05">05</option><option value="06">06</option><option value="07">07</option><option value="08">08</option><option value="09">09</option><option value="10">10</option><option value="11">11</option><option value="12">12</option></select> / <select name="year" ><option value="">yyyy</option><option value="2006">2006</option><option value="2007">2007</option><option value="2008">2008</option><option value="2009">2009</option><option value="2010">2010</option><option value="2011">2011</option><option value="2012">2012</option><option value="2013">2013</option><option value="2014">2014</option><option value="2015">2015</option></select></td>
	</tr>
	<tr>
		<td class="fieldname" valign="top">Category</td>
		<td class="required" valign="top" align="center"></td>
		<td valign="top" class="data"><select name="category"" ><option value="">-None-</option><option value="A">A</option><option value="B">B</option><option value="C">C</option><option value="D">D</option><option value="E">E</option></select></td><td><span class="small">A - Work based learning<br/>B - Professional activity<br/>C - Formal / Educational<br/>D - Self directed learning<br/>E - Other activities e.g. voluntary work<br/>A document detailing the Activity Categories can be found <a href="downloads/cpd_categories.pdf">here</a>.</span></td>
	</tr>
	<tr>
		<td class="fieldname" valign="top"></td>
		<td class="required" valign="top" align="center"></td>
		<td class="data"><br/>
		<input type="submit" class="button1" value="Save"></td>
	</tr>
	<tr><td></td><td>&nbsp;&nbsp;&nbsp;&nbsp;</td><td></td></tr>
</table>
</form>

Open in new window

I need to add an "edit" option for this data, and it should look like the original form - with the same drop-down options.

How can I display stored data in the form when it offers itself for editing?
 
0
hnmcc
Asked:
hnmcc
  • 2
1 Solution
 
CombatGold1Commented:
If I understood you correctly in your edit form page (or code) you are already retrieving the information, but you need your drop-down list to have the value stored already selected when the edit form loads?

All you need to do is add this to the attributes of the <option> you want selected:
selected="selected"

Open in new window

...which would become...
<option value="04" selected="selected">04</option>

Open in new window

...if the day stored was 04.

Assuming you are printing your options with a while loop, I'm guessing a while (mysql_fetch_assoc/array()), you could use something like this in place of line 7:
<td class="data">
	<select name="day">
		<option value="" disabled="disabled">dd</option>
<?php
	for ($i = 1; $i <= 31; $i++)
	{
		if ($i == $myRecord['day']) ?
			$selected = ' selected="selected"';
		else
			$selected = null;

		echo "\t\t<option value=\"${i}\"${selected}>${i}</option>\n";
	}
?>
	</select> / <select name="month">
		<option value="" disabled="disabled">mm</option>
<?php
	for ($i = 1; $i <= 12; $i++)
	{
		if ($i == $myRecord['month'])
			$selected = ' selected="selected"';
		else
			$selected = null;

		echo "\t\t<option value=\"${i}\"${selected}>${i}</option>\n";
	}
?>
	</select> / <select name="year">
		<option value="" disabled="disabled">yyyy</option>
<?php
	for ($i = 2006; $i <= 2015; $i++)
	{
		if ($i == $myRecord['year'])
			$selected = ' selected="selected"';
		else
			$selected = null;

		echo "\t\t<option value=\"${i}\"${selected}>${i}</option>\n";
	}
?>
	</select>
</td>

Open in new window

0
 
hnmccAuthor Commented:
Accepted solution worked well, with a little bit of cleaning up and formatting added.

The completed code for days looks like this:

	$i = 1; 	
	while ( $i <= 31 ) {
		//Chooses the stored day
		if ($i == $this_day)
 			$selected = ' selected="selected"';
 		else
 			$selected = null;
 		//Adds a leading zero 1-9
		$day = sprintf("%02d",$i);
 		print "<option value=\"$day\"$selected>$day</option>";
		$i++; 	
 	}

Open in new window

Thanks!
0
 
hnmccAuthor Commented:
Just what I needed.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now