Solved

ASP - problem while "refresh" the screen

Posted on 2001-06-21
15
287 Views
Last Modified: 2011-08-18
I am using the following samepage.asp to add name and age of a person to a database. When the "submit" is pressed the values are stored to the database after simple validation. If the user refresh the screen, again the same data is added into the database. How to restrict this?

=============samepage.asp===============
<% @Language="VBSCRIPT"%>
<%
 cName=request.querystring ("uname")
 if cName <> "" then
     ' get the values from the querystring
     ' add to database.
 end if
%>

<html>
<BODY>
<form name="f" action="samepage.asp" method="get">
<table>
<tr>
          <td>User Name</td>
          <td> <input type="text" name="uname"></td>
          <td>Age</td>
          <td> <input type="text" name="uage"></td>
</tr>
<tr>
     <td><input type="reset" value="Cancel"> </td>
     <td><input type="submit" name="SaveFile" value="Save" OnClick="return checking()" ></td>
</tr>
</table>
</form>
</body>
</html>
<script language="javascript">

function checking()
{

  if(document.f.uname.value=='' || document.f.uage.value=='')
  {
    alert("FAILURE: \nRequired fields are empty...! ");
     return false;
  }
return true;
}
</script>
==================end===============
help me.
0
Comment
Question by:arogyarajxavier
  • 5
  • 4
  • 2
  • +4
15 Comments
 
LVL 33

Expert Comment

by:hongjun
ID: 6216667
First of all you cannot restrict the Refresh dialog from appearing. However, you can do a simple trick on it.

I see that you are actually submitting to the same page. Try this to solve your problem.
<% @Language="VBSCRIPT"%>

<%
' Add this one
Response.Buffer = True
%>

<%
cName=request.querystring ("uname")
if cName <> "" then
    ' get the values from the querystring
    ' add to database.

    ' Add this one
    Response.Redirect "samepage.asp"
end if
%>

hongjun
0
 
LVL 2

Expert Comment

by:Fenatu
ID: 6216685
Use a seperate page for putting the stuff into the database. Then if you want, response.redirect back to the samepage.
0
 
LVL 33

Accepted Solution

by:
hongjun earned 20 total points
ID: 6216687
Another method is the submit the result to another page.
<form name="f" action="another_page.asp" method="get">
Then in another_page.asp, do this
<%
cName=request.querystring ("uname")
if cName <> "" then
    ' get the values from the querystring
    ' add to database.

    ' Add this one
end if
%>
<script language="JavaScript">
<!--
    window.document.location.replace("third_page.asp");
//-->
</script>


hongjun
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:arogyarajxavier
ID: 6216713
Its fine hongjun. but the problem is it wouldnot dispaly message like "Record is added", because after adding it loaded a fresh page immediately. so the user couldnot know whether the record is inserted or not. Thanks.
0
 
LVL 5

Expert Comment

by:nilapenn
ID: 6216920
You can very well give an alert in the javascript
<script language="JavaScript">
<!--
   alert("Record added sucessfully");
   window.document.location.replace("third_page.asp");
//-->
</script>


or you can pass a querystring to the third page and print the message in the thirdpage.

<script language="JavaScript">
<!--
  window.document.location.replace("third_page.asp?success=true");
//-->
</script>

then in thirdpage.asp

<%
if request.querystring("success")="true" then
Response.Write("Record Added Sucessfully")
end if
%>

0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 6216941
If you don't mind cookies, there's always setting a session variable on the data submit page.  At the the start of the page entry just check the value of the session variable...

If Session("entered") <> "T" Then
   <Submit Data>
   Session("entered") = "T"
Else
   <Error - Data Already Submitted>
End If
0
 

Author Comment

by:arogyarajxavier
ID: 6216974
leew, ur suggestion is good, but using this tech. we can insert record only once, if we need to add another then load the page again. thanks
0
 
LVL 7

Expert Comment

by:weesiong
ID: 6216998
arogyarajxavier,

nilapeen comments is suit to you :)

Regards,
Wee Siong
0
 

Author Comment

by:arogyarajxavier
ID: 6217004
thanks weesiong, put the stuff in two separate pages is the solution right?
0
 
LVL 7

Expert Comment

by:weesiong
ID: 6217122
arogyarajxavier,

yes, i more like nilapeen this comments, post with querystring

<script language="JavaScript">
<!--
 window.document.location.replace("third_page.asp?success=true");
//-->
</script>

then in thirdpage.asp

<%
if request.querystring("success")="true" then
Response.Write("Record Added Sucessfully")
end if
%>

actually if you want continue submit the data, it should be form.asp and non third_page.asp

The points for nilapeen :)

Regards,
Wee Siong
0
 
LVL 5

Expert Comment

by:raizon
ID: 6218475
This is what I do for situations like this.

1.  Before inserting data into the db I check to see if it already exists on something that I know will be unique like

strSQL = "SELECT * FROM table WHERE fname = '" & strFName & '" AND lname = '" & strLName & "' AND address = '" & strAddress & '"

set rst = conn.execute(strSQL)

If rst.EOF Then
  insert data
Else
  show that data already exists
End If

Either that or you can make your primary key be a group of fields such as a combination of fname, lname, and address.
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6218753
Ya. Definitely send to another page is a better choice as shown on my second method.

hongjun
0
 

Author Comment

by:arogyarajxavier
ID: 6226642
Thank you hongjun, for your timely respone. Thank you nilapenn and others who helped me a lot.
0
 

Author Comment

by:arogyarajxavier
ID: 6226654
thanks hongjun
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6231201
Glad that I can help here.

hongjun
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XML Parsing Classic ASP 5 56
Select case on click 3 14
JS does not refresh 6 16
innerHTML 7 10
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 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