Self Posting Form in ASP with CSS (No Tables!)

This simple form posts to itself, has form fields prefilled that erase themselves on focus (with javascript enabled), and will retain the fields values if the user does not fill in all the fields correctly. It uses CSS to alert the user to missing information.

There are also some handy, dandy ASP functions that are good to put into a master include file so any form can use them.

Of course, this can easily be converted to PHP, and I will be posting that later.

Code:

<% 

dim defaultarr, ispost, ix, field, inputvalue, required, message, thestring, requiredarr
dim firstname, lastname, age

defaultarr = array("John","Doe","21")

'this bit usually goes into a master include file
if Request.ServerVariables("REQUEST_METHOD") = "POST" then
       ispost = true
elseif Request.ServerVariables("REQUEST_METHOD") = "GET" then
       isget = true
else
       isget = false
       ispost = false
end if



if ispost then
      For ix = 1 to Request.Form.Count
      field = request.form.key(ix)
      inputvalue = request.form.item(ix)
      if inputvalue = "" then
         required = dorequired(required,field)
      elseif inputvalue = defaultarr(ix-1) then
         required = dorequired(required,field)
      end if
      TheString = field & "= Request.Form(""" & field & """)"
      Execute(TheString)
    next
    
    requiredarr = split(required,", ")
    if required <> "" then
       message = ucase(left(required,1)) & mid(required,2) & " " & makeplural("is ",ubound(requiredarr)+1) & " required"
    end if
    
    if age <> "" then
       if cint(age) < 21 then
          message = domessage(message,"You must be over 21")
          required = dorequired(required,"age")
       end if
    end if

else
        firstname = defaultarr(0)
        lastname = defaultarr(1)
        age = defaultarr(2)
end if

if message <> "" then
        %>
        <script type="text/javascript">
      <!--
        alert('<%=message%>');
      //-->
      </script>
      <%
        message = "<div class=" & chr(034) & "message" & chr(034) & "><strong>" & message & "</strong></div>"  
        end if
    
end if    

'these functions also go into a master include file
function dorequired(current, field)
    if instr(current,field) > 0 then
       'do nothing it's already there
   else
        if current <> "" then
           dorequired = current & ", " & field
        else
           dorequired = field
        end if
    end if
end function

function domessage(current,message)
    if instr(current,message) > 0 then
       'do nothing it's already there
   else
        if current <> "" then
           domessage = current & ", " & message
        else
           domessage = message
        end if
    end if
end function

function makeplural(word,quantity)

    if word = "is " and quantity <> 1 then
       makeplural = "are "
    elseif word = "is " and quantity = 1 then
       makeplural = "is "
    elseif word = "have " and quantity = 1 then
       makeplural = "has "
    elseif word = "have " and quantity <> 1 then
       makeplural = "have "
    elseif word <> "is " and quantity <> 1 then
       makeplural = word & "s"
    else
       makeplural = word
    end if

end function

 %>

<style type="text/css">
<% 'this bit can go into a separate include file 
if required <>' "" then %>
<% for i = 0 to ubound(requiredarr) %>
#<%=requiredarr(i)%>1 {
                font-weight:bold; 
                background-color: yellow; 
                color:red
                }
#<%=requiredarr(i)%> {
               background-color: pink; 
               color: black
               }
<% next %>
<% end if %>
</style>
</head>
<body>
<form action="<%=request.servervariables("script_name")%>" method="post">
<fieldset><legend>Agent Information</legend>
<label for="firstname" id="firstname1">First Name: </label> <input type="text" name="firstname" id="firstname" value="<%=firstname%>" <%if isget then%>onfocus="if(this.value == '<%=firstname%>') this.value = '';"<%end if %>  /><br />
<label for="lastname" id="lastname1"> Last Name:</label> <input type="text" name="lastname" id="lastname" value="<%=lastname%>" <%if isget then%>onfocus="if(this.value == '<%=lastname%>') this.value = '';"<%end if %>   /><br />
<label for="age" id="age1">Age: </label> <input type="text" name="age" id="age" value="<%=age%>" <%if isget then%>onfocus="if(this.value == '<%=age%>') this.value = '';"<%end if %> />
<input type="submit" value="Submit" />
</fieldset>
</form>