Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Validation Question

Status
Not open for further replies.
Feb 8, 2002
43
US
Below is a snippet of code I am working on in a relatively simple project. I have a form that request 2 fields. In the vbscript I validate whether or not it exist and so on. My problem comes in when I try and add the data to the recordset. This prog simply adds IDs and Passwords to a rec set. It is not working. I initiate valid to false. In the function Validate() in it is changed to true. But for some reason, it isn't adding. I am new to this and would appreciate any help. Thanks.


<%@ Language = &quot;VBScript&quot;%>
<%
Public Valid
valid = false
%>
<HTML>
<HEAD>
<META name=VI60_defaultClientScript content=VBScript>

<TITLE>AIR</TITLE>
<SCRIPT LANGUAGE=VBscript>
<!--option explicit
function Validate()

dim Logon
dim Flag
valid = false
Flag = false
Logon = document.AIR1.txtLogonID.value
Password = document.AIR1.txtPassword.value
if Logon = &quot;&quot; then
msgbox &quot;Enter in Your Logon ID&quot;,,&quot;No Logon ID&quot;
else
if Isnumeric(Logon) then
msgbox &quot;There must me alpha characters in your Logon ID&quot;,,&quot;Invalid Data&quot;
else
if password = &quot;&quot; then
msgbox &quot;No Password was Entered&quot;,,&quot;Need Password&quot;
else
flag = true
valid = true
end if
end if
end if

end function
-->
</SCRIPT>
</HEAD>
<BODY bgColor=silver>
<FORM METHOD=&quot;post&quot; NAME=&quot;AIR1&quot;>
<p>&nbsp;</p>
<strong><u>AIR</u></strong>
<P>
Logon ID: <INPUT NAME=&quot;LogonID&quot; id=txtLogonID><br>
Password: <INPUT NAME=&quot;Password&quot; id=txtPassword> </P>

<P id=frmAIR> <br>&nbsp;</P>
<input id =cmdSubmit type=submit value=submit name=button1 onclick = validate()>
</FORM>

<%
if valid = true then
'Declaring variables
'Opening and connecting to data
dim conn
dim rs
dim strID
dim strconn

'set a local variable to my DSN-less connection String
strconn = &quot;DRIVER=Microsoft Access Driver (*.mdb);DBQ=&quot; & Server.MapPath(&quot;TPL.mdb&quot;)

'Create the Connection object
set conn = server.createobject(&quot;adodb.connection&quot;)
conn.open strconn

'Create the recordset object
set rs = server.createobject(&quot;adodb.recordset&quot;)
'This statement opens the table so we can add a record notice the addnew
'The 2, 2 is how the table is opened there are many ways it can be opened
rs.open &quot;Logon&quot;, conn, 2, 2

'Use the addnew method of the recordset object to add a record
rs.addnew
'Set the table column = to my input text box from my form
rs(&quot;LogonID&quot;) = request(&quot;LogonID&quot;)
rs(&quot;Password&quot;) = request(&quot;Password&quot;)
rs.update
'I do a movelast here to get the ID that is automatically generated
'I also set the value to a local variable so I can write out to the database
rs.movelast
end if
%>







<%

'Always! Always set your objects to nothing. This clears them out of servers memory
'Your network admins will like this

set rs= nothing
set conn = nothing
%>

</BODY>
</html>
 
I noticed your missing the {} in the connection string. it may be the problem
'set a local variable to my DSN-less connection String
strconn = &quot;DRIVER={Microsoft Access Driver (*.mdb) };DBQ=&quot; & Server.MapPath(&quot;TPL.mdb&quot;)

there's some steps you can take to find errors here
first response.write to the screen the following
connection string
recordset from DB to see if you're connecting
form values after validation to check for changes or content.

it is possible in some instances that when performing validations that you inherritly change the value of the form fields and thus causing errors down the line.

also if LogonID is a unique ID you may be causing errors here
try adding a unique ID along with these fields if you do not have it this way.
A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
Thanks for your repsonse. But the connection is fine, I've tested that same code on a simpler page and it works. I think the problem is that nothing inside the server tags are being read, I'm not sure though.
 
I pinpointed the problem. When the page loads it runs through the the server side code if I take the if valid = true statement out. Once I do the function from the submit onclick I have no way of telling the server to run the server code, the sever thinks it is done. Issue: How do I run the server code after I execute my validation function?
 
submit the form to itself by putting the name of the page in the action attrib of the form tag A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
just tried that. It still isn't working. Anymore suggestions would be appreciated. Thanks for you help.
 
You cannot execute server-side code once it has finished and sent the page to the user, why not write your validate function as a server-side function, have the page direct back to itself,
if the Request.Form(&quot;LoginID&quot;) <> &quot;&quot; then
run the verification function.
If they verify
Response.Redirect to the correct page
otherwise
display your error message

display login form at bottom regardless (as your doing above).

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top