Page 1 of 1

Dropdowns, selected and editing documents

Posted: Sun Jun 18, 2006 7:28 am
by freefall
Hi, I've been curious about this for ages but how do you go about ensuring your select list has the correct value marked as selected when updating a forum.

Say for example I have made a form with a month field on it as a select list. The user selects the month, saves the document (form) to mysql. Later that day they then decide to edit the data. When they go into the document (form) the values have obviously been prepopulated from the db including the month being marked as SELECTED (ie the corresponding value stored in the db).

This might be the simplest question answer known to man I dont know, but I've written 3 complex bits of code to resolve it. One of which I'm 50% happy with, the other 2 I wanna burn.

Any ideas?

ps hi all btw joined today, been reading for hours, great site.

Posted: Sun Jun 18, 2006 9:54 am
by ambivalent
Compare the month returned by the DB to the select list, act on the results:

Code: Select all

if($monthToSelect == $monthPrePopulated)
	{
		echo "<option value=\"$monthToSelect\" selected=\"selected\">$monthToSelect</option>\n";
	}
		else
	{
		echo "<option value=\"$monthToSelect\">$monthToSelect</option>\n";
	}

Posted: Sun Jun 18, 2006 9:51 pm
by freefall
2 things with that

a) if you store that list of variables in a relational db you will store the id not text

b) I dont want to store every list in the db.