Solved

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

Posted on 2011-03-17
3
187 Views
Last Modified: 2012-06-27
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
Comment
Question by:hnmcc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
CombatGold1 earned 500 total points
ID: 35156844
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
 

Author Comment

by:hnmcc
ID: 35171764
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
 

Author Closing Comment

by:hnmcc
ID: 35171768
Just what I needed.
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question