PDA

View Full Version : sending string to a function


ahmedsoliman
12-08-2002, 12:37 AM
if i have the following function that check if the option's value of dropdown box equal to specefic value in my DB or not,
if is it equal the option will be selected.
the problem is when send a value without put it into " " it dosen't work.
for example: rs3(0)=2, i=2

function selectedsize (firstval)

if firstval=rs3(0) then
selectedsize="selected"

end if
end function

<option value=2 <%=selectedsize("2")%>>L</option>-------->selected

<option value=<%=i%> <%=selectedsize(i)%>><%=i%></option>-------->isn't selected

i have to call my function with avariable ,how to make the previous line work and be selected.

whammy
12-08-2002, 03:33 AM
Hmm,

Try:

Function selectedsize(byVal firstval)

although that seems like a strange way to do this, I'd usually just do something like:


<% Response.Buffer = True %>
<%
Dim indent, indent2, i
indent = " "
indent2 = indent & indent
%>
<html>
<head></head>
<body>
<form id="monthform" action="months.asp" method="post">
<select name="month">
<%
For i = 1 to 12
Response.Write(indent2 & "<option value=""" & Right("00" & i,2) & """")
If Right("00" & i,2) = Request.Form("month") Then Response.Write(" selected=""selected""")
Response.Write(">" & MonthName(i) & "</option>")
If i < 12 Then Response.Write(vbCrLf)
Next
%>
</select>
<input type="submit" value="Submit">
</form>
</body>
</html>

ahmedsoliman
12-09-2002, 12:40 AM
thank you whammy, but it's difficult way,iwill type it 12 th time (as your example) instead of creating loop and send it as avariable.
i thought there are simple solution to send (i) equal 2 like sending "2", i tried to (cstr(i)) but dosn't work too,don't woory.

whammy
12-09-2002, 12:52 AM
Ok, I see what you're getting at now:

<%
Function IsSelected(val1,val2)
If val1 = val2 Then IsSelected = " selected=""selected"""
End Function

asdf = Request.Form("asdf")
%>
<form id="foobar" action="blah.asp" method="post">
<select name="asdf">
<% For i = 1 to 4 %> <option value="<% = i %>"<% = IsSelected(CStr(i),asdf) %>><% = i %></option><% If i < 4 Then Response.Write vbCrLf %><% Next %>
</select>
<input type="submit" value="Submit" />
</form>


Output (if you select 4):

<form id="foobar" action="blah.asp" method="post">
<select name="asdf">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4" selected="selected">4</option>
</select>
<input type="submit" value="Submit" />
</form>


:D

Is that what you mean? Just pass the database value as one of the the variables passed to the function instead of Request.Form("asdf").

P.S. That's a very good idea, it might shorten some of my scripts a bit - thanks for the idea! ;)