PDA

View Full Version : submit to self


ekrub
06-23-2003, 07:38 PM
I'm trying to submit a form to itself, for validation, and to enter data into a database.

What I need to find out though, is how do I stop the validation from running before the form is even submitted (when the user now hits the form the first time) Should I use a cookie, or what?

aghill
06-23-2003, 07:51 PM
Include a hidden field in the form that holds a number; put the code to process the form inside a If... then statement.

If (Request.Form("sub") = 1) then
Dim con
Set con = .....

etc...etc...

End if

ekrub
06-23-2003, 07:55 PM
I'm really not too sure exactly what you're saying there. Forgive me. But I'm a bit new to ASP. Can you elaborate?

aghill
06-23-2003, 08:46 PM
I understand....

Ok, so you've got a form... let's say that you're writing a name to a db.

<form name="form1" method="post" action="this_page.asp">
<input type="hidden" name="ReplyForm" value=1>
<INPUT type="text" name="name">
<input type="submit" value="submit" name="submit">
</form>

Now for the part where you run a sql statement and write the information to a db.

<%
If (Request.Form("ReplyForm") = 1) then
dim con
set con = Server.CreateObject("ADODB.Connection")
con.Open "DSN=DB"

dim sql
sql = "insert into Names (name) values ('" & Request.Form("name") & "')"

con.Execute sql
con.close
set con = nothing
End if
%>

ekrub
06-23-2003, 08:51 PM
I understood that, but what I don't get is... Doesn't the variable in the hidden input field still make the asp code execute the first time it loads?

Cause no matter how many times u refresh, or if its the first time you go to the page, the value is still "1"

Frank
06-23-2003, 10:22 PM
That value will only be set to 1 when the form is submitted. Otherwise it will be (empty, null, blank, whatever you want to call it :) ) But here is how I do that:


<% Option Explicit %>
<% Response.Buffer = True %>

<%
'<ASP>
'<Dimension variables>
Dim Mode
Dim Username, Password

'<Business Code>
Mode = Request.Form("Mode")

If Mode = "Submit" Then
Username = Request.Form("Username")
Password = Request.Form("Password")
Mode = ""

'Do your form field validation and database writing here

End If

'</Business Code>
'</ASP>
%>
<html>

<head>
<title>Page Title</title>
</head>

<body>
<form name="FormName" method="post" action="PageName.asp">

<b>Username:</b>&nbsp;
<input type="text" name="Username" size="20">

<b>Password:</b>&nbsp;
<input type="password" name="Password" size="20">

<p>
<input type="submit" name="mode" value="Submit">
<input type="reset" name="reset" value="Clear">
</p>

</form>
</body>


Simplistic, but it works for me :)

HTH
Frank

ekrub
06-24-2003, 07:41 AM
Hey, thanks! I only understood it after I left the computer last night. Guess I was tired or something...

This is tiring.. this asp thing. I hate having to type everything! And so much!

Frank
06-24-2003, 07:13 PM
Originally posted by ekrub
Hey, thanks! I only understood it after I left the computer last night. Guess I was tired or something...

This is tiring.. this asp thing. I hate having to type everything! And so much!

Wait till you've been doing it for 3 years :} The good side to that is that once you have written quite a few ASP apps, you learn how to make the code more and more modular. Eventually you get to the point where you are just plugging in sections of code you wrote for other apps to make a new app.