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
Solved

ASP - problem while "refresh" the screen

Posted on 2001-06-21
15
293 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JSON error 4 70
Select only the top record in a left join 13 45
Cannot open file from ASP if user is "logged in" 13 30
innerHTML 7 34
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

791 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