Get Adobe Flash player
Second Sighting Logo

Using Stored Values With Select Elements

Posted in PHP by Tom on Jan 9th 2008

Heres how to use regular expressions to transform a plain HTML select element into one that autoselects based on a previous choice. You could use this to retrieve a value from a database and automatically select the correct option in a dropdown menu. The select element in the example below is called occupation and it is stored in an array called $user_data.

search for:

<option value=”(.*)”>.*</option>

replace with:

<option value=”$1″<?php if ($user_data['occupation'] == “$1″) { echo ‘ selected=”selected”‘; } ?>>$1</option>

Make sure to rename the fields accordingly for your situation.

Comments are closed.