2012-03-31 67 views
0

我的問題的快速總結:我想顯示用戶填寫的字段數。填寫計數字段

我是學習ASP的新手,我看過並沒有找到解決方案。我生成了一個簡單的示例頁面,與我需要幫助的內容相比很簡單,但是同樣的想法。

當用戶轉到第一頁時,他顯然會得到三個文本框。 當他提交表格時,他會顯示他提交的信息,填寫的字段數量。我正在嘗試遍歷每個字段,如果數字大於0,則將其添加到名爲tt的計數器。

通過2顯示我的循環,而不是給我tt的價值。我嘗試使用response.write進行循環,但沒有奏效。

<html> 
<body> 

<% 
sub pass1 
%> 
Pass 1 <P> 
<form action="count_p.asp" method = "post"> 
<input type="text" name="t1"><BR> 
<input type="text" name="t2" ><BR> 
<input type="text" name="t3"><BR> 
<input type="hidden" name="token" value="2"> 
<input type="submit" value="submit query"> 




<% 
end sub 

sub pass2 
    response.write "<P>Pass 2 tokenvalue="+cstr(tokenvalue) 

t1=request.form("t1") 
t2=request.form("t2") 
t3=request.form("t3") 



response.write "<P>t4=" + t1 
response.write "<P>t4=" +t2 
response.write "<P>t4=" +t3 
%> 

tt=0 
for i=1 to 3 
    if t + cstr(i) > 0 then 
    tt=tt+1 
    end if 
then 


response.write "<P>Fields filled = " + tt 



<% 
end sub 



tokenvalue=request.form("token") 
select case tokenvalue 
case "" 
    call pass1 
case "2" 
    call pass2 
case "3" 
    call pass3 

end select 
%> 


</body> 
</head> 
+0

是你的問題在傳統的ASP或ASP.net?代碼看起來像ASP。 – 2012-03-31 02:08:18

+0

看起來可能是使用VB的MVC2? – TGH 2012-03-31 02:13:38

+0

.asp,很抱歉 – user1084561 2012-03-31 02:19:03

回答

0

您不能使用動態變量名稱。他們不受支持。試試這個:

'Here we are splitting all the form values 
'into an array. Your values will come in 
'looking something like this: 
' 
' t1=4&t2=323&t3=3 
' 
'after we split them, you'll have 3 sets 
'of values that look like: 
' 
' aFormNamesAndValues(0) = "t1=4" 
' aFormNamesAndValues(1) = "t2=323" 
' aFormNamesAndValues(2) = "t3=3" 

aFormNamesAndValues = Split(Request.Form,"&") 

tt=0 
for i=0 to 2 
    'Ok, splitting once again, this time on the 
    'equals character. Now we will have an array 
    'with 2 values, the name of the form field 
    'and the value it holds, we can check each 
    'value and perform some logic on it: 
    aNameAndAValue = Split(aFormNamesAndValues(i),"=") 

    if aNameAndAValue(0) = "t" & (i+1) then 
     if aNameAndAValue(1) > 0 then 
      tt=tt+1 
     end if 
    end if 
then 
0

可以使用Eval方法本 - 通常它是令人難以接受的,但在這種情況下,它是有效的用法:

tt=0 
For i=1 to 3 
    curValue = Eval("t" & i) 
    If IsNumeric(curValue) Then 
     If CLng(curValue)>0 Then 
      tt = tt + 1 
     End If 
    End If 
Next 

正如你也可以看到,您需要將值轉換使用CLng編號以進行適當的比較。