Solved

ASP - problem while "refresh" the screen

Posted on 2001-06-21
15
281 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

19 Experts available now in Live!

Get 1:1 Help Now