Solved

ASP - problem while "refresh" the screen

Posted on 2001-06-21
15
269 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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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 …
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now