Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

drop down selections 1

Status
Not open for further replies.

secretsquirrel

Programmer
Mar 22, 2001
202
GB
i've got a form which, when filled out incorrectly, displays the form again populating the fields with what the user submitted.

it's easy enough for most of my fields...

Code:
<input name="firstName" value="<?=$firstName?>">

...etc.

the problem i've got is that i don't know how to display a drop down menu with a specific value selected, based on a users submission. obviously i can do the following...

Code:
<select name="title">
   <option value="Mr">Mr</option>
   <option value="Mrs">Mrs</option>
   <option value="Miss" selected>Miss</option>

...but this won't work dynamically. it's almost as if i need to write <select name="title" selected="<?=$title?>">, but that's clearly gibberish!

does anyone know how i can achieve this?

thanks in advance,

ss...
 
I've done this with Javascript in an onload function:

Code:
function fillOnLoad() {
   
	document.updateForm.selectField.value = "<%=insHowHeard%>";

}

cheers :)

tigerjade


Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live. -- Martin Golding


 
And dynamically with ASP could be
Code:
<%
strTitle = request.form("title")

dim i
dim bSelected
dim strArrayTitle(2)

strArrayTitle(0) = "Mr"
strArrayTitle(1) = "Mrs"
strArrayTitle(2) = "Miss"
%>

'...  etc


<select name="title">

<%
for i = 0 to ubound(strArrayTitle)
if strTitle = strArrayTitle(i) then
   bSelected = "selected"
else
   bSelected = ""
end if
response.write "<option value='" & strArrayTitle(i) & "' " & bSelected"> " & strArrayTitle(i) & vbCrLf
next
%>
</select>

this would then easily allow other options for title to be added to the array


Chris.

Indifference will be the downfall of mankind, but who cares?
 
tigerjade,

do you know how i'd do the same thing with a check box?

thanks in advance,

ss...
 
np, secret :)

i wrote it so you can pass in the form's name as well as the names of the checkboxes & the value you're pulling from the db:

Code:
function fillCheckBox(formName,name,param) {

   // formName is the name of the form (shock!)
   // name is name of checkboxes
   // param is value of preferred checkbox

   var tempForm = eval("document." + formName + "." + name);

   for (i = 0; i < tempForm.length; i++) {
      if (tempForm[i].value == param) {
         tempForm[i].checked = true;
      }
   }
}

enjoy!

tigerjade :)






"Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top