Solved

issue with form using classic asp.

Posted on 2009-07-09
10
310 Views
Last Modified: 2012-06-27
Hi,

I apologize in advance as I'm new to asp as i'm usually a windows forms developer. I've got the following code: -

<form method="get" action="testcopy.asp">
Serial No: <input type="text" name="fname" /><br />
Model No: <input type="text" name="lname" /><br />
<input type="submit" value="Submit" />
</form>

<%
IF NOT Request.QueryString("fName")<>"" THEN
    %><text><font color=red><%response.Write ("Serial Number is Missing") %></font></text><br /><%
END IF

IF NOT Request.QueryString("lName")<>"" THEN
    %><text><font color=red><%response.Write ("Model Number is Missing") %></font></text><%
END IF
%>

When the page loads for the first it comes up with both Serial Number is Missing and Model Number is Missing which I presume it will do with the above code as both text boxes are empty when the page loads. How do I make it so that it does not show when the page loads and does when the submit button is pressed.

Many Thanks
Lee
0
Comment
Question by:ljhodgett
10 Comments
 
LVL 8

Expert Comment

by:lharrispv
ID: 24813643
Put the code into a function in the head of the html page and then an onSubmit to the form

<%
Fucntion checkForm ()
IF NOT Request.QueryString("fName")<>"" THEN
    %><text><font color=red><%response.Write ("Serial Number is Missing") %></font></text><br /><%
END IF

IF NOT Request.QueryString("lName")<>"" THEN
    %><text><font color=red><%response.Write ("Model Number is Missing") %></font></text><%
END IF
End Function
%>

Your HTML would look like this:

<form method="get" action="testcopy.asp" onsubmit="return checkForm()" >
Serial No: <input type="text" name="fname" /><br />
Model No: <input type="text" name="lname" /><br />
<input type="submit" value="Submit" />
</form>
0
 

Author Comment

by:ljhodgett
ID: 24813908
Hi,

I've put it in the page as follows: -




<!--#include file="../../config.inc.asp"-->
<%
'* System admin form, online access to Line Configurator
'* This page allows users with admin permissions to reconfigure the equipment
'* that is used on a specific production line

'check login information
If session("username") = "" and request.querystring("viewonly")=""  Then                                    ' if not logged in at all      
    Response.Redirect "/test/loginscript.asp?MessageNo=1&WhereURL=" & request.servervariables("URL")    
end if

if Session("bAdmin")<>"True" and request.querystring("viewonly")=""  then
      response.write("Insufficient permission level. User must be an admin to perform Line Configuration")
      response.end
end if



if not (Session("LocalUserID") = 13 OR Session("LocalUserID") = 79) then
      response.write("Insufficient permission level. Only Lee & Rob have permission to view form")
      response.end
end if

bIgnoreTab=True

%>
<html>
<head>
<title>Test Engineering - Register Test copy</title>
<link rel="stylesheet" href="/basestyle.css" type="text/css">

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<%
Fucntion checkForm ()
IF NOT Request.QueryString("fName")<>"" THEN
    %><text><font color=red><%response.Write ("Serial Number is Missing") %></font></text><br /><%
END IF

IF NOT Request.QueryString("lName")<>"" THEN
    %><text><font color=red><%response.Write ("Model Number is Missing") %></font></text><%
END IF
End Function
%>


</head>
<%

Dim Conn, sqlRecordSet,cLineID
set Conn = Server.CreateObject("ADODB.Connection")
Conn.open global_TestServer
Conn.CommandTimeOut=0

Set rs = Server.CreateObject("ADODB.RecordSet")            ' set recordset object  
rs.CursorLocation = 3                                    ' use client cursor location as there will be lots of scanning

cQueryString="Select SerialNumber, ModelNumber, convert(varchar(20),Last_Tested,3) + ' ' + convert(varchar(20),Last_Tested,108)  as Last_Tested, Username FROM dbo.TestCopy left JOIN Users      ON  dbo.TestCopy.Tested_By = Users.LocalUserID order by ModelNumber"

rs.Open cQueryString,Conn, 2, 1                        ' execute query
%>

<table width = "100%">
    <tr>
        <td><center><font size="4"><B>Test Copy Registration</B></font></center></td>
    </tr>
    <tr>
        <td><center><font size="3">This webpage shows all current registered test copies within test control</font></center></td>
    </tr>
</table>
</br>


            <table border = 0 cellspacing = 1 width = "100%">
                  <tr>
                    <td width="17%"><center><font size="2"><B>Test Copy Serial Number</B></font></center></td>
                    <td width="43%"><center><font size="2"><B>Test Copy Model Number</B></font></center></td>
                    <td width="17%"><center><font size="2"><B>Last Tested</B></font></center></td>
                    <td width="20%"><center><font size="2"><B>Tested By</B></font></center></td>
                  </tr>
                 
                  <%
                  do while not RS.eof
                  %>

                            <tr bgcolor="lightblue">
                                <td><Left><font size="2"><%response.write RS("SerialNumber")%>&nbsp;</font></center></td>
                                <td><center><font size="2"><%response.write RS("ModelNumber")%></font></center></td>
                                <td><center><font size="2"><%response.write RS("Last_Tested")%></font></center></td>
                                <td><center><font size="2"><%response.write RS("Username")%></font></center></td>
                            </tr>                      
                 
                  <%
                  RS.movenext
                  loop
                  %>
              </table>

<form method="get" action="testcopy.asp" >
<center>
    <table border =1 >
        <tr>
            <td>
            <center><font color ="blue" >Register New Test Copy</font></center>
            </td>
        </tr>
        <tr>
            <td>
                <table>
                    <tr>
                        <td>
                            Serial No:
                        </td>
                        <td>
                            <input type="text" name="fname" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Model No:
                        </td>
                        <td>
                            <input type="text" name="lname" size ="80" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input type="submit" value="Submit" />
                        </td>
                    </tr>
                </table>
            </td>    
        </tr>
    </table>
</center>

 <br />

</form>

   
  <BR>
  <HR/>
  <%
  rs.Close
  set rs = nothing
  Conn.Close
  set Conn = nothing

%>

</BODY>
</html>

But it comes up with an error saying: -

Microsoft VBScript compilation error '800a0400'

Expected statement

/test/admin/ManagerPanel/testcopy.inc.asp, line 45

End Function

Many Thanks for your help.
Lee

0
 
LVL 8

Expert Comment

by:lharrispv
ID: 24813975
function in the opening function declaration is spelled wrong.. that might be the problem....
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:ljhodgett
ID: 24814108
lol, I've sorted that one out but when I click submit it does not print serial number not found or model number not found with the following code:-




<!--#include file="../../config.inc.asp"-->
<%
'* System admin form, online access to Line Configurator
'* This page allows users with admin permissions to reconfigure the equipment
'* that is used on a specific production line

'check login information
If session("username") = "" and request.querystring("viewonly")=""  Then                                    ' if not logged in at all      
    Response.Redirect "/test/loginscript.asp?MessageNo=1&WhereURL=" & request.servervariables("URL")    
end if

if Session("bAdmin")<>"True" and request.querystring("viewonly")=""  then
      response.write("Insufficient permission level. User must be an admin to perform Line Configuration")
      response.end
end if



if not (Session("LocalUserID") = 13 OR Session("LocalUserID") = 79) then
      response.write("Insufficient permission level. Only Lee & Rob have permission to view form")
      response.end
end if

bIgnoreTab=True

%>
<html>
<head>
<title>Test Engineering - Register Test copy</title>
<link rel="stylesheet" href="/basestyle.css" type="text/css">

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<%
Function checkForm ()
IF NOT Request.QueryString("fName")<>"" THEN
    %><text><font color=red><%response.Write ("Serial Number is Missing") %></font></text><br /><%
END IF

IF NOT Request.QueryString("lName")<>"" THEN
    %><text><font color=red><%response.Write ("Model Number is Missing") %></font></text><%
END IF
End Function
%>


</head>
<%

Dim Conn, sqlRecordSet,cLineID
set Conn = Server.CreateObject("ADODB.Connection")
Conn.open global_TestServer
Conn.CommandTimeOut=0

Set rs = Server.CreateObject("ADODB.RecordSet")            ' set recordset object  
rs.CursorLocation = 3                                    ' use client cursor location as there will be lots of scanning

cQueryString="Select SerialNumber, ModelNumber, convert(varchar(20),Last_Tested,3) + ' ' + convert(varchar(20),Last_Tested,108)  as Last_Tested, Username FROM dbo.admin_TestCopy_Register left JOIN dbo.admin_Users      ON  dbo.admin_TestCopy_Register.Tested_By = dbo.admin_Users.LocalUserID order by ModelNumber"

rs.Open cQueryString,Conn, 2, 1                        ' execute query
%>

<table width = "100%">
    <tr>
        <td><center><font size="4"><B>Test Copy Registration</B></font></center></td>
    </tr>
    <tr>
        <td><center><font size="3">This webpage shows all current registered test copies within test control</font></center></td>
    </tr>
</table>
</br>


            <table border = 0 cellspacing = 1 width = "100%">
                  <tr>
                    <td width="17%"><center><font size="2"><B>Test Copy Serial Number</B></font></center></td>
                    <td width="43%"><center><font size="2"><B>Test Copy Model Number</B></font></center></td>
                    <td width="17%"><center><font size="2"><B>Last Tested</B></font></center></td>
                    <td width="20%"><center><font size="2"><B>Tested By</B></font></center></td>
                  </tr>
                 
                  <%
                  do while not RS.eof
                  %>

                            <tr bgcolor="lightblue">
                                <td><Left><font size="2"><%response.write RS("SerialNumber")%>&nbsp;</font></center></td>
                                <td><center><font size="2"><%response.write RS("ModelNumber")%></font></center></td>
                                <td><center><font size="2"><%response.write RS("Last_Tested")%></font></center></td>
                                <td><center><font size="2"><%response.write RS("Username")%></font></center></td>
                            </tr>                      
                 
                  <%
                  RS.movenext
                  loop
                  %>
              </table>

<form method="get" action="testcopy.asp" onsubmit="return checkForm()"  >
<center>
    <table border =1 >
        <tr>
            <td>
            <center><font color ="blue" >Register New Test Copy</font></center>
            </td>
        </tr>
        <tr>
            <td>
                <table>
                    <tr>
                        <td>
                            Serial No:
                        </td>
                        <td>
                            <input type="text" name="fname" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Model No:
                        </td>
                        <td>
                            <input type="text" name="lname" size ="80" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input type="submit" value="Submit" />
                        </td>
                    </tr>
                </table>
            </td>    
        </tr>
    </table>
</center>

 <br />

</form>

   
  <BR>
  <HR/>
  <%
  rs.Close
  set rs = nothing
  Conn.Close
  set Conn = nothing

%>

</BODY>
</html>

Best Regards
Lee
0
 
LVL 8

Expert Comment

by:lharrispv
ID: 24814303
let's try moving this to the onClick event for the submit button rather than onSubmit.


remove the onSubmit from the form and add onClick"checkForm()" to the submit button.  I will test here too while are are testing there.
0
 

Author Comment

by:ljhodgett
ID: 24814368
Still no joy i'm afraid. Thanks for trying your end too.

Lee
0
 
LVL 8

Expert Comment

by:lharrispv
ID: 24814391
no worries... testing for a solution as we speak
0
 
LVL 8

Expert Comment

by:lharrispv
ID: 24814656
theoretically th is should work but I cannot seem to figure out why... maybe someone else can chime in and show us both where we are going wrong?  I simplied your page just to make it easier for me to test..

<html>
<head>
<SCRIPT LANGUAGE="VBScript">
<!---
Sub testbutton_OnClick()
if (document.formtest.fname.value="") and (document.formtest.lname.value="") THEN
   
   msgbox("test")
   <text><font color=red><%response.Write ("Serial Number is Missing")</font></text><br />
   Exit Sub
   Else
   document.formtest.submit
   
END IF
End Sub
<---
</script>

</head>
<body>
<form method="post" name = "formtest" action="testcopy.asp" >

 
            <center><font color ="blue" >Register New Test Copy</font></center>
       
                            Serial No:
                       
                            <input type="text" name="fname" />
                     
                            Model No:
                       
                            <input type="text" name="lname" size ="80" />
                     
                            <INPUT TYPE="Button" NAME="testbutton" VALUE="Submit">
 

</form>
</body>
</html>
0
 
LVL 9

Expert Comment

by:rg20
ID: 24815023
it appears your mixing javascript with ASP

I would do something like this
its javascript and allows you to do more clientside validations such as is the serial number > 1 or length > 5 etc...
<html>
<head>
<SCRIPT LANGUAGE="Javascript">
<!---
function validate(){
   if (document.formtest.fname.value="") ||(document.formtest.lname.value="") {
      alert ("Serial Number is Missing");
      return false;
   }
   return true;
}
</script>

</head>
<body>
<form method="post" name = "formtest" action="testcopy.asp" onsubmit="return validate(this)">

 
            <center><font color ="blue" >Register New Test Copy</font></center>
       
                            Serial No:
                       
                            <input type="text" name="fname" />
                     
                            Model No:
                       
                            <input type="text" name="lname" size ="80" />
                     
                            <INPUT TYPE="Button" NAME="testbutton" VALUE="Submit">
 

</form>
</body>
</html>
0
 
LVL 3

Accepted Solution

by:
harris9999 earned 500 total points
ID: 24821910
You could do a check to see if the form was actually submitted.  
I assume the form is being submitted to the page itself.
So first check if the form has been submitted then do the validation

<%
IF NOT Request.QueryString("fName")<>"" THEN
    %><text><font color=red><%response.Write ("Serial Number is Missing") %></font></text><br /><%
END IF

IF NOT Request.QueryString("lName")<>"" THEN
    %><text><font color=red><%response.Write ("Model Number is Missing") %></font></text><%
END IF
%>



<form method="get" action="testcopy.asp">
Serial No: <input type="text" name="fname" /><br />
Model No: <input type="text" name="lname" /><br />
<input type="hidden" name="frmSubmit" value="1" /><
<input type="submit" value="Submit" />
</form>


<%
IF request.querystring("frmSubmit")=1 then
IF NOT Request.QueryString("fName")<>"" THEN
    %><text><font color=red><%response.Write ("Serial Number is Missing") %></font></text><br /><%
END IF
 
IF NOT Request.QueryString("lName")<>"" THEN
    %><text><font color=red><%response.Write ("Model Number is Missing") %></font></text><%
END IF
END IF
%>
 
 
 
<form method="get" action="testcopy.asp">
Serial No: <input type="text" name="fname" /><br />
Model No: <input type="text" name="lname" /><br />
<input type="hidden" name="frmSubmit" value="1" /><
<input type="submit" value="Submit" />
</form>

Open in new window

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question