Jump to content

how can modify this code


Recommended Posts

hi all i write the following code to validate a form

<%Function ValidateField(sFieldValue, sFieldType) Dim bFieldIsOkay bFieldIsOkay = True Select Case LCase(sFieldType)  Case "name"  If trim(Len(sFieldValue)) = 0 Then bFieldIsOkay = False  Case "email"  If Len(sFieldValue) < 5 Then    bFieldIsOkay = False  Else    If Instr(1, sFieldValue, " ") <> 0 Then    bFieldIsOkay = False    Else    If InStr(1, sFieldValue, "@") < 2 Then      bFieldIsOkay = False    Else      If InStrRev(sFieldValue, ".") < InStr(1, sFieldValue, "@") + 2 Then      bFieldIsOkay = False      End If    End If    End If  End If    Case "address"  If Len(sFieldValue) = 0 Then bFieldIsOkay = False  Case "city"  If Len(sFieldValue) = 0 Then bFieldIsOkay = False  Case "state"  If Len(sFieldValue) <> 2 Then bFieldIsOkay = False  Case "zip"  If Len(sFieldValue) <> 5 And Len(sFieldValue) <> 10 Then    bFieldIsOkay = False  End If      Case Else   bFieldIsOkay = False End Select ValidateField = bFieldIsOkayEnd FunctionSub ShowFormField(strField) %> <TR>  <TD ALIGN="right"><B><%= strField %>:</B> </TD>  <TD><INPUT NAME="<%= strField %>" TYPE="text" VALUE="<%= Request.Form(strField) %>"></INPUT></TD>  <TD><%  If dictFields(LCase(strField)) Then  Response.Write "<IMG SRC=""C:\Inetpub\wwwroot\a.jpg"" BORDER=""0"" WIDTH=""25"" HEIGHT=""25"">"  End If  %></TD> </TR> <%End Sub%><%Dim Field      'looping variableDim dictFields 'dictionary for failed fieldsSet dictFields = Server.CreateObject("Scripting.Dictionary")  For Each Field in Request.Form If ValidateField(Request.Form(Field), Field) = False Then  dictFields.Add LCase(Field), True End IfNext 'FieldIf Request.Form.Count <> 0 And dictFields.Count = 0 Then??????????????????????????? %><BR> <BR> <B>Here's what you entered:</B><BR> <% For Each Field In Request.Form  Response.Write Field & ": " & Request.Form(Field) & "<BR>" & vbCrLf Next 'FieldElse If Request.Form.Count <> 0 Then  %>  make sure thats your data is correct .  <% End If %> <FORM ACTION="<%= Request.ServerVariables("Script_Name") %>" METHOD="post" NAME="TheForm"> <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0"> <% ShowFormField("Name") ShowFormField("Email") ShowFormField("Address") ShowFormField("City") ShowFormField("State") ShowFormField("Zip") %> </TABLE> <INPUT TYPE="reset" VALUE="Reset The Form"></INPUT> <INPUT TYPE="submit" VALUE="Submit The Form"></INPUT><BR> <BR> </FORM> <%End If%>
where the chr "?" showen i need a code thats when the user enter a vaild data post his data to a data base the problem is ,, i cant get the form value or i cant make the action of the form on anthor page ,, how i cam post this data to anthor page .
Link to post
Share on other sites

When I tried this code I did not have any problem getting values from the form, it seems to get and display the values I entered in the form, when all the info is correct..This is what i got with your code.Here's what you entered:Name: aEmail: a@yahoo.comAddress: aCity: aState: aaZip: 12345

Link to post
Share on other sites

hi i need a code thats post these values to anthor page , the problem i face is the action on the server varible not to anthor page for example not to chk.asp i need a code can post these server varible to anthor page when the user enter a correct data he will show what he enter then i need a submit button thats post the data to anthor page . thx all

Link to post
Share on other sites

I'll say again, you need to change the form action.<form action="chk.asp"> will submit to chk.asp<form action="submit.asp"> will submit to submit.asphttp://www.w3schools.com/tags/tag_form.aspWhat you have [Request.ServerVariables("Script_Name")] will always submit to the same page.

Link to post
Share on other sites

hi man i use the server variable to validate the form values cause when i make the action to anthor page if the user enter a correct a data he will still in the add.asp page so the user will not confirmed that the regestreation action is completed . this is a simple code*******reg.asp<form name = "form" method "post" action = "add.asp"><input type = "text" name = "user"><input type = "pass" name = "pass"></form>*******add.aspdim userdim pass user = request.form("user")pass = requset.form("pass")if user = "" thenresponse.write("enter the user name")end ifif pass = "" thenresponse.write("enter the password")end ifhere the code of the adding data to the database*********when i use this code if the user enter a correct data he will still in add.aspif you can solve this code i will use this method . thank you :)

Link to post
Share on other sites

It's probably easiest to have everything on the same page. Have the page that shows the login fields also be the page that adds them to the database. That way, you can check on the top of the page if they submitted the form, and if they did, you can check their username and password. If they are fine, you add them to the database and either give them a welcome message or redirect them to another page, and if they have an error you can just show them the error message and give them the login form again.If you want to do it with 2 pages, then you will want your second page to redirect to the first page with an error message.

if user = "" thenresponse.redirect("reg.asp?error=" & server.URLEncode("enter a name"))end ifif pass = "" thenresponse.redirect("reg.asp?error=" & server.URLEncode("enter the password"))end if

Then you need the reg.asp to look for an error variable in the querystring and display the message.

Link to post
Share on other sites

hi man thank u very much for this useful idea if u tell me how

Then you need the reg.asp to look for an error variable in the querystring and display the message.
cause my mind cant understand this step but the prev step work very good plz help me ,, this is a graduation project i dont have more time thx for ur help
Link to post
Share on other sites

hi man i write this code in reg.asp

<html><form method="post"  name=form action = "add.asp">  <b>enter you name </b><br>  <input type="text" name="name" value="<%=Request("name")%>"><br>  <input type="submit" value="submit"  name=submit></form><% If Len(Request("name"))= 0 then %><%response.write("enter correct data")%>  <%=Server.UrlEncode(Request("name"))%><% End IF %>
and this code in add.asp
<%dim namedim avg_secname = request.form("name")if name = "" thenresponse.redirect("d.asp?error=" & server.URLEncode("enter a name"))end if%>
its work good if u have and extra information in this code ur welcome
Link to post
Share on other sites

I don't think you need this in reg.asp:

<% If Len(Request("name"))= 0 then %><%response.write("enter correct data")%>  <%=Server.UrlEncode(Request("name"))%><% End IF %>

You aren't submitting the data to reg.asp, so the name will never be there.You need to look for the error message and display that:

<% If Len(Request("error")) > 0 then %><%response.write(Request("error"))%><% End IF %>

Also, have add.asp redirect back to reg.asp, not to d.asp.

Link to post
Share on other sites

hi man the u give me is work very very good but there are moe problem when the page show the error the other field is cleared why ?for example if the user enter the name correctly and the avg correctly but the schools name invalid ..when he press submit he will see the error msg but the all filed will clear what i should doing ?regards

Link to post
Share on other sites

Well, you will need to move all the data back to the first page and then show it again. This is really much easier if you are only using 1 page, then you don't have to move any data around.

Link to post
Share on other sites

<% MODE = Request.Form("mode") IF MODE = "Check" THEN This is where your validation, SQL Update code will be. END IF %><html><form action="Currentpage.asp" method="post" name="frm">HTML Code for textbox etc...<input type="Hidden" name="mode" value="Check" /></form></html>The top ASP code will not be executed in the first time, since the IF condition will fail. But when the submit button is clicked, the page is reloaded but by that time the hidden textbox will get the value and will go through IF condition. This will make the top ASP code to execute.HTH

Link to post
Share on other sites

I think I understand your original request now.I added the following to your script:

<form name="myForm" action="somepage.asp" method="post">	<%For Each Field In Request.Form%>  <input type="hidden" name="<%=Field%>" value="<%=request(Field)%>" />	<%Next%></form><script language="vbscript"><!--myForm.submit()//--></script>

The whole script:

<%Function ValidateField(sFieldValue, sFieldType)Dim bFieldIsOkaybFieldIsOkay = TrueSelect Case LCase(sFieldType)  Case "name"  If trim(Len(sFieldValue)) = 0 Then bFieldIsOkay = False  Case "email"  If Len(sFieldValue) < 5 Then    bFieldIsOkay = False  Else    If Instr(1, sFieldValue, " ") <> 0 Then    bFieldIsOkay = False    Else    If InStr(1, sFieldValue, "@") < 2 Then      bFieldIsOkay = False    Else      If InStrRev(sFieldValue, ".") < InStr(1, sFieldValue, "@") + 2 Then      bFieldIsOkay = False      End If    End If    End If  End If    Case "address"  If Len(sFieldValue) = 0 Then bFieldIsOkay = False  Case "city"  If Len(sFieldValue) = 0 Then bFieldIsOkay = False  Case "state"  If Len(sFieldValue) <> 2 Then bFieldIsOkay = False  Case "zip"  If Len(sFieldValue) <> 5 And Len(sFieldValue) <> 10 Then    bFieldIsOkay = False  End If      Case Else   bFieldIsOkay = FalseEnd SelectValidateField = bFieldIsOkayEnd FunctionSub ShowFormField(strField)%><TR>  <TD ALIGN="right"><B><%= strField %>:</B> </TD>  <TD><INPUT NAME="<%= strField %>" TYPE="text" VALUE="<%= Request.Form(strField) %>"></INPUT></TD>  <TD><%  If dictFields(LCase(strField)) Then  Response.Write "<IMG SRC=""C:\Inetpub\wwwroot\a.jpg"" BORDER=""0"" WIDTH=""25"" HEIGHT=""25"">"  End If  %></TD></TR><%End Sub%><%Dim Field      'looping variableDim dictFields 'dictionary for failed fieldsSet dictFields = Server.CreateObject("Scripting.Dictionary")  For Each Field in Request.FormIf ValidateField(Request.Form(Field), Field) = False Then  dictFields.Add LCase(Field), TrueEnd IfNext 'FieldIf Request.Form.Count <> 0 And dictFields.Count = 0 Then'???????????????????????????%><BR><BR><B>Here's what you entered:</B><BR><%For Each Field In Request.Form  Response.Write Field & ": " & Request.Form(Field) & "<BR>" & vbCrLfNext 'Field%><form name="myForm" action="somepage.asp" method="post">	<%For Each Field In Request.Form%>  <input type="hidden" name="<%=Field%>" value="<%=request(Field)%>" />	<%Next%></form><script language="vbscript"><!--myForm.submit()//--></script><%ElseIf Request.Form.Count <> 0 Then  %>  make sure thats your data is correct .  <%End If%><FORM ACTION="<%= Request.ServerVariables("Script_Name") %>" METHOD="post" NAME="TheForm"><TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0"><%ShowFormField("Name")ShowFormField("Email")ShowFormField("Address")ShowFormField("City")ShowFormField("State")ShowFormField("Zip")%></TABLE><INPUT TYPE="reset" VALUE="Reset The Form"></INPUT><INPUT TYPE="submit" VALUE="Submit The Form"></INPUT><BR><BR></FORM><%End If%>

That script will redirect all the variables to another location (somepage.asp).However you could just put this in it's place:

server.Transfer("somepage.asp")

It'll do the same thing

Link to post
Share on other sites

hi all thank u all for ur help ,, realy i am very happy cause ur Attention and ur ideaswill i want u to see this script ,, i make the regsteratin action and check action and the addtion to the db in the same page ,, see this code ..

<%Function ValidateField(sFieldValue, sFieldType) Dim bFieldIsOkay bFieldIsOkay = True Select Case LCase(sFieldType)  Case "name"  If trim(Len(sFieldValue)) = 0 Then bFieldIsOkay = False  Case "mail"  If Len(sFieldValue) < 5 Then    bFieldIsOkay = False  Else    If Instr(1, sFieldValue, " ") <> 0 Then    bFieldIsOkay = False    Else    If InStr(1, sFieldValue, "@") < 2 Then      bFieldIsOkay = False    Else      If InStrRev(sFieldValue, ".") < InStr(1, sFieldValue, "@") + 2 Then      bFieldIsOkay = False      End If    End If    End If  End If  case "s"   If (sFieldValue) = "a" Then bFieldIsOkay = False  Case "address"  If Len(sFieldValue) = 0 Then bFieldIsOkay = False  case "la"    If Len(sFieldValue) = 0 Then bFieldIsOkay = False  Case "city"  If Len(sFieldValue) = 0 Then bFieldIsOkay = False  Case "state"  If Len(sFieldValue) <> 2 Then bFieldIsOkay = False  Case "zip"  If Len(sFieldValue) <> 5 And Len(sFieldValue) <> 10 Then      bFieldIsOkay = False    End If    Case Else 'if an unknown type gets in reject form!  bFieldIsOkay = False End Select ValidateField = bFieldIsOkayEnd FunctionSub ShowFormField(strField) %> <TR>  <TD ALIGN="right"><B><%= strField %>:</B> </TD>  <TD><INPUT NAME="<%= strField %>" TYPE="text" VALUE="<%= Request.Form(strField) %>"></INPUT></TD>  <TD><%  If dictFields(LCase(strField)) Then  Response.Write "<IMG SRC=""IMAGES22.JPG"" BORDER=""0"" WIDTH=""25"" HEIGHT=""25"">"  End If  %></TD> </TR> <%End Sub%><%Dim Field      'looping variableDim dictFields 'dictionary for failed fieldsSet dictFields = Server.CreateObject("Scripting.Dictionary")  For Each Field in Request.Form If ValidateField(Request.Form(Field), Field) = False Then  dictFields.Add LCase(Field), True End IfNext 'FieldIf Request.Form.Count <> 0 And dictFields.Count = 0 Then Dim conn            Dim rs  Set Conn = Server.CreateObject("ADODB.Connection")conn.Provider="Microsoft.Jet.OLEDB.4.0"conn.Open(Server.Mappath("/webdata/tst.mdb"))set rs = Server.CreateObject("ADODB.recordset")      RS.open "samer",Conn,2,2rs.AddNewrs.Fields("Name") = Request.Form("name")rs.Fields("email") = Request.Form("email")rs.Fields("address") = Request.Form("address")rs.Fields("city") = Request.Form("city")rs.Fields("state") = Request.Form("state")rs.Fields("zip") = Request.Form("zip")rs.Updaters.CloseSet rs = NothingSet Conn = NothingResponse.Redirect "thx.asp"%>  <% For Each Field In Request.Form  Response.Write Field & ": " & Request.Form(Field) & "<BR>" & vbCrLf Next 'FieldElse If Request.Form.Count <> 0 Then  %>  plese check your data  <% End If %> <FORM ACTION="<%= Request.ServerVariables("Script_Name") %>" METHOD="post" NAME="TheForm"> <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0"> <%  ShowFormField("name") ShowFormField("mail") ShowFormField("address") ShowFormField("city") ShowFormField("state") ShowFormField("zip") %>  </TABLE> <INPUT TYPE="reset" VALUE="Reset The Form"></INPUT> <INPUT TYPE="submit" VALUE="Submit The Form"></INPUT><BR> <BR> </FORM> <%End If%>
will but again i face one small proplem how i can add a dropdown menu to this
<FORM ACTION="<%= Request.ServerVariables("Script_Name") %>" METHOD="post" NAME="TheForm"> <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0"> <%  ShowFormField("name") ShowFormField("mail") ShowFormField("address") ShowFormField("city") ShowFormField("state") ShowFormField("zip") %>  </TABLE> <INPUT TYPE="reset" VALUE="Reset The Form"></INPUT> <INPUT TYPE="submit" VALUE="Submit The Form"></INPUT><BR> <BR> </FORM> <%
when i add a dropdown menu its will be out of scoop of the table its will be the first item befor name fieldhow can add it ???thanks all :)
Link to post
Share on other sites

I don't understand what the problem is. You can put the dropdown anywhere you want. You can have it before the table:

<select name="dropdown"><option /></select><table>...</table>

Or in the table:

<table>...<tr><td><select name="dropdown"><option /></select></td></tr></table>

Or after the table:

<table>...</table><select name="dropdown"><option /></select>

Link to post
Share on other sites

hi i mean when i add the dropdown menu inside the table area its will be out of the scoop of the table ,, like this gender : name :...etc i want it like thatname .... etcgender i will try put it after the table tag :)

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...