?
Solved

How do I take html javascript-verified form input, send it to an ASP page and then write it to a server file?

Posted on 2003-02-21
6
Medium Priority
?
215 Views
Last Modified: 2013-12-16
Basically, I am trying to create a simple contact database using a plaintext file.  I would like users to be able to fill out an HTML form with their full name, phone number, etc and then have this data automatically verified using Javascript and then entered into this plaintext file on the server.  

Additionally, I would like a way to then be able to view this file from a web page as well.

I would use Oracle or Access, however it takes a long time in my company to gain approval for this, thus I hope to shorten the turnaround time by using a text file which is located in the same server directory as the html file.  In theory, I would love to be able to write to a javascript array in a .js file on the server as this would be easy to then manipulate and view.  I would also like to be able to write to the client's browser upon successful addition of their information.

I know that server side file i/o is impossible using purely javascript, and i previously read about posting to an ASP page to accomplish this.


Any help would be greatly appreciated!

-Eric
0
Comment
Question by:amherstprep
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 4

Expert Comment

by:whammy
ID: 7996637
Are you familiar with FileSystemObject using ASP?

http://www.w3schools.com/asp/asp_ref_filesystem.asp
0
 

Author Comment

by:amherstprep
ID: 8010725
I am familiar with FileSystemObject, but what I am at loss for is how to interact between ASP and Javascript.
0
 
LVL 4

Accepted Solution

by:
whammy earned 300 total points
ID: 8012493
Ok, this doesn't have any javascript validation, but will give you an idea how to write to/read from a text file (it does have server-side validation using ASP/VBScript for email address, however you'd have to add the other fields you want to your form):

addtolist.asp:
==============

<% @Language="VBScript" %>
<% Option Explicit %>
<%
'*************************************** FUNCTIONS

Function IsEmail(str)
   Dim ieRegEx
   Set ieRegEx = New RegExp
   ieRegEx.Pattern = "^[\w\+\'\.-]+@[\w\'\.-]+\.[a-zA-Z0-9]{2,}$"
   IsEmail = ieRegEx.Test(str)
End Function

'*********************************** DIM VARIABLES

Dim Email, SubmitNum ' Requested variables
Dim Found            ' Variable to check for duplicate emails
Dim fs, f, filepath  ' FileSystemObject variables

'******************** REQUEST/INITIALIZE VARIABLES

Email = Request.Form("Email")
SubmitNum = Request.Form("SubmitNum")
filepath = Mid(Server.MapPath("\"), 1, InStrRev(Server.MapPath("\"),"\")-1) & "\database\emaillist.txt"

'************************************ MAIN PROGRAM

SubmitNum = SubmitNum + 1

If IsEmail(Email) Then
   Call LookupEmail()
   If Found = False Then
      Call AppendEmail()
      Call DisplayThankYou()
   Else
      Call DisplayDupeMessage()
   End If
Else
   Call DisplayForm()
End If

'******************************** END MAIN PROGRAM

'************************************* SUBROUTINES

Sub LookupEmail()
   Found = False
   Set fs = CreateObject("Scripting.FileSystemObject")
   Set f = fs.OpenTextFile(filepath,1,True)
   Do While NOT f.AtEndOfStream AND Found = False
      If f.ReadLine = Email Then Found = True
   Loop
   Set f = Nothing
   Set fs = Nothing
End Sub

Sub AppendEmail()
   Set fs = CreateObject("Scripting.FileSystemObject")
   Set f = fs.OpenTextFile(filepath,8,True)
   f.WriteLine(Email)
   Set f = Nothing
   Set fs = Nothing
End Sub
%>

<% Sub DisplayThankYou() %><?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
   <meta http-equiv="refresh" content="3;URL=addtolist.asp" />
   <title>Thank You!</title>
</head>
<body>
   <div><span style="font-weight:bold">Thank you for subscribing!</span></div>
</body>
</html><% End Sub %>


<% Sub DisplayDupeMessage() %><?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
   <meta http-equiv="refresh" content="3;URL=addtolist.asp" />
   <title>Your email address already exists!</title>
</head>
<body>
   <div><span style="font-weight:bold">Your email address already exists in our database!</span></div>
</body>
</html><% End Sub %>


<% Sub DisplayForm() %><?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
   <title>Please enter your email address to subscribe!</title>
</head>
<body>
   <form id="addtoemaillist" method="post" action="addtolist.asp">
      <input type="text" name="Email" value="<% = Email %>" />
      <% If SubmitNum > 1 Then %>
      <span style="color:#ff0000"> * Invalid Email Address</span>
      <% End If %>
      <br />
      <input type="hidden" name="SubmitNum" value="<% = SubmitNum %>" />
      <input type="submit" value="Submit" />
   </form>
</body>
</html><% End Sub %>

readfromlist.asp:
=================

<% @Language="VBScript" %>
<% Option Explicit %>
<% Response.CacheControl="Private" %>
<% Response.Expires = -1 %>
<%
'*********************************** DIM VARIABLES

Dim fs, f, filepath  ' FileSystemObject variables
Dim filecontents     ' Contents of the file

'******************** REQUEST/INITIALIZE VARIABLES

filepath = Mid(Server.MapPath("\"), 1, InStrRev(Server.MapPath("\"),"\")-1) & "\database\emaillist.txt"

'************************************ MAIN PROGRAM

Call GetEmailList()
Call DisplayEmailList()

'******************************** END MAIN PROGRAM

'************************************* SUBROUTINES

Sub GetEmailList()
   Set fs = CreateObject("Scripting.FileSystemObject")
   Set f = fs.OpenTextFile(filepath,1,True)
   If NOT f.AtEndOfStream Then
      filecontents = f.ReadAll
   End If
   Set f = Nothing
   Set fs = Nothing
End Sub
%>

<% Sub DisplayEmailList() %><?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
   <title>Email List</title>
</head>
<body>
   <div>
      <form id="form1" action="javascript://">
         <textarea name="emaillist" cols="50" rows="10"><% = filecontents %></textarea>
      </form>
   </div>
</body>
</html><% End Sub %>

This should give you a pretty good idea how to use text files in combination with ASP, and this script has been tested so I know it works just fine.

The only thing you may need to change is this:

filepath = Mid(Server.MapPath("\"), 1, InStrRev(Server.MapPath("\"),"\")-1) & "\database\emaillist.txt"

I map the path out to a directory ABOVE my wwwroot folder, so the .txt file cannot be downloaded directly. If you wanted it in the same folder you'd just use:

filepath = Server.MapPath("emaillist.txt")

 :-)

0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 4

Expert Comment

by:whammy
ID: 8012504
P.S. Of course your server must support ASP for this.
0
 
LVL 4

Expert Comment

by:whammy
ID: 8029763
I guess this worked ok for you then? :)

If you need help with javascript validation on the client-side, post any problems you have here, I use both javascript and ASP for validation all the time (since I do this for a living).

It's best to always, always validate server-side; but if you can validate client-side _and_ it's an application that could potentially have many people accessing it at the same time (such as people responding to an email broadcast, for instance), then it's not a bad idea to use client-side javascript to catch validation errors before another request is sent to the server, so you save bandwidth and/or server resources.
0
 

Author Comment

by:amherstprep
ID: 8034047
yes, thanks ;)

i will definitely take you up on that offer if i run into problems.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

When setting up new project requests for our site, one of the most powerful tools our team has available to use is Axure (http://www.axure.com/). It’s a tool for creating software and web prototypes that can function and interact as if it were the a…
Objective of This Article In 1990’s, when I was a budding software professional, I had a lot of confusion about which stream or technology, I had to choose to build my career. In those days, I had lot of confusion like whether to choose System so…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

770 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