Este post contém um exercício resolvido em sala, cujo objetivo é calcular o fatorial de um número fornecido por um usuário mediante um formulário.
O código abaixo ilustra a utilização de funções definidas pelo programador (”fatorial2″, neste caso) e da coleção Form do objeto Request.
Copiem este código e executem-no, analisando seu funcionamento:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
Function fatorial2(valor)
Dim i, mult
mult = valor
For i = valor-1 To 1 Step -1
mult = mult * i
Next
fatorial2 = mult
End Function
Dim valor, fatorial
valor = Request.Form("valor")
If (valor <> "") Then
fatorial = fatorial2(valor)
End If
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<%
If (valor <> "") Then
Response.Write("Resultado: " & fatorial)
End If
%>
<form id="form1" name="form1" method="post" action="">
<label for="textfield">Fatorial de:</label>
<input name="valor" type="text" id="valor" size="4" maxlength="4" />
<label for="Submit"></label>
<input type="submit" name="Submit" value="Calcular" id="Submit" />
</form>
</body>
</html>
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
Dim endereco
' Obtendo o endereço especificado no formulário
endereco = Request.Form("endereco")
' Se o endereço não estiver em branco, redirecionar o usuário
If (endereco <> "") Then
Response.Redirect(endereco)
End If
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<label for="textfield">URL:</label>
<input name="endereco" type="text" id="endereco" size="60" />
<label for="Submit"></label>
<input type="submit" name="Submit" value="Ir" id="Submit" />
</form>
</body>
</html>