Jump to content

refresh the page


jojay

Recommended Posts

It depends on what kind of "database" we're taling about. A counter? Well, instead of having detect every single "hit" on the page, the server's script could instead create a sesson variable and count the sessons intead. Or fetch the header information, and count the different IPs.

Link to comment
Share on other sites

It depends on what kind of "database" we're taling about. A counter? Well, instead of having detect every single "hit" on the page, the server's script could instead create a sesson variable and count the sessons intead. Or fetch the header information, and count the different IPs.

Hi,I am working on asp with MS Access. I have a form and trying to connect to the database. It works fine. so, the first page has a form with submit button. when hit the submit button, it goes to resultpage.asp, in which I have put the database connection and recordset codes and I have a table of all the data posted in the form as response.write on the resultpage.asp. I have a print button to print that page. Now, if I hit the refresh button, it again submits the same data to the database. database specs:ID - autonumber - primary key.Any help would be great. Thanks.
Link to comment
Share on other sites

When you first enter the page create a session and put in the current date and time.Then when you enter the page again (pressing refresh etc) check the session and if the page was sumbitted within the last 1day 1week or whatever reject it.If you know what i mean :)

Link to comment
Share on other sites

When you first enter the page create a session and put in the current date and time.Then when you enter the page again (pressing refresh etc) check the session and if the page was sumbitted within the last 1day 1week or whatever reject it.If you know what i mean :)

If you dont mind, can you give an example. I havent used session. thanks.
Link to comment
Share on other sites

Sessions: http://www.w3schools.com/asp/asp_sessions.aspto put the current date/time in a session named date

session("date")=now()

To retrieve the session and check it agains current date time

dim thedatethedate=datediff("s",session("date"),now())

control structure

if (thedate<120) then     'submitted page less than 2 mins agoresponse.write "error"end if

something along those lines

Link to comment
Share on other sites

Sessions: http://www.w3schools.com/asp/asp_sessions.aspto put the current date/time in a session named date
session("date")=now()

To retrieve the session and check it agains current date time

dim thedatethedate=datediff("s",session("date"),now())

control structure

if (thedate<120) then     'submitted page less than 2 mins agoresponse.write "error"end if

something along those lines

I dont know if I did it right but it doesnt do anything except add the error msg along with my stuff. I added the above code on top of the resultpage.asp, that is the second page, and it didnt do anything and still was add records to the database.
Link to comment
Share on other sites

You can always insert a database processing page.form.asp fill out the form and submitprocess.asp insert into the database, response.redirect to result.asp, or back to form.aspSince the process page does a response.redirect, they will be refreshing the redirected page, not the page that did the processing.

Link to comment
Share on other sites

You can always insert a database processing page.form.asp   fill out the form and submitprocess.asp  insert into the database, response.redirect to result.asp, or back to form.aspSince the process page does a response.redirect, they will be refreshing the redirected page, not the page that did the processing.

That is how it is now.I have form.asp and submit. That information is going to resultpage.asp in which the processing is taking place using Addnew method. But I dont have response.direct in the result page to the same page. If i do response.redirect("resultpage.asp"), will the content remain in the page? Because on the resultpage.asp, I am also response.write all the info which was entered on form.asp, so if I want to print the info, can use the print command.
Link to comment
Share on other sites

It's probably best to have form.asp submit to process.asp, and have process put everything in the database and then redirect to something like result.asp, and send result the ID number of the record that was just inserted in the database. That way, process can look up the info from the database and display it.

Link to comment
Share on other sites

It's probably best to have form.asp submit to process.asp, and have process put everything in the database and then redirect to something like result.asp, and send result the ID number of the record that was just inserted in the database.  That way, process can look up the info from the database and display it.

Sounds good. But can you tell at which point will I response.redirect the result of ID from process.asp to result.asp. That is, should it come somewhere in the beginning or at end.
Link to comment
Share on other sites

At the end. Here is my uber-detailed schematic of process.asp:

<get responses from POST> username = request.form("username")<validate responses> user entered a string for a number? bad user! smack them to the ground!<insert into database><get id for new data>response.redirect("thanks.asp?id=" + new_id)

Since you will probably want to validate the variables, it might be a good idea to have the form page do all the processing also. So, the form page would show the form, and would also do the above, get all the data, validate it, insert into the database, and redirect. You can go one step further, and also have the form page redirect back to itself. That way you only use 1 page (although the code might be a little confusing). It doesn't matter which page you redirect to, as long as a redirect happens. That way they refresh the redirected page (which didn't do any processing) instead of the page that inserted into the db.

Link to comment
Share on other sites

At the end.  Here is my uber-detailed schematic of process.asp:
<get responses from POST> username = request.form("username")<validate responses> user entered a string for a number? bad user! smack them to the ground!<insert into database><get id for new data>response.redirect("thanks.asp?id=" + new_id)

Since you will probably want to validate the variables, it might be a good idea to have the form page do all the processing also.  So, the form page would show the form, and would also do the above, get all the data, validate it, insert into the database, and redirect.  You can go one step further, and also have the form page redirect back to itself.  That way you only use 1 page (although the code might be a little confusing).  It doesn't matter which page you redirect to, as long as a redirect happens.  That way they refresh the redirected page (which didn't do any processing) instead of the page that inserted into the db.

Thank you very much. I wouldnt have had a better explanation. But one thing I dont understand - <get id for new data>. Is it like this: <input type="hidden" name="id"> to redirect to same page or to different page?
Link to comment
Share on other sites

Hi,I tried to work this around and I am almost there but with little problem. What I did is, is to keep the form.asp and direct the result to result.asp. In result.asp, i am giving the response.redirect to resultpage.asp and also included the <input type="hidden" name="ID">. In resultpage.asp, i am trying to get the results from form.asp through the hidden ID. In resultpage.asp, the codes are:

ID = Request.form("ID")<input type="hidden" name="ID" value="<%response.write("ID")%>">then table with form fields as response.write(request.form("fieldname"))

As you can see, I havent given connection to the database since I am just extracting the result to this page through response.write. But I am getting an error such as below:"object required".what does it mean? Do I need connection in this page? thanks.

Link to comment
Share on other sites

Hi,I tried to work this around and I am almost there but with little problem. What I did is, is to keep the form.asp and direct the result to result.asp. In result.asp, i am giving the response.redirect to resultpage.asp and also included the <input type="hidden" name="ID">. In resultpage.asp, i am trying to get the results from form.asp through the hidden ID. In resultpage.asp, the codes are:
ID = Request.form("ID")<input type="hidden" name="ID" value="<%response.write("ID")%>">then table with form fields as response.write(request.form("fieldname"))

As you can see, I havent given connection to the database since I am just extracting the result to this page through response.write. But I am getting an error such as below:"object required".what does it mean? Do  I need connection in this page? thanks.

I understand what it means. How will I get the ID from previous page. This is my scenerio:
Form.aspID field as hiddenother formfields....Submit button------result.aspformfields = request.form("formfields")Set connection..Set Recordset...addnew method to insert to database<input type="hidden" name="CID" value="<%=rs("CID")%>">connection.closerecordset.closeresponse.redirect("resultpage.asp")-------resultpage.aspCID = Request.Form("CID")response.write("formfields")Print command to print.

Now, since I have given autonumber as datatype for CID, each time when I sent the data to the database, the autonumber increments. How will I get the CID from the database to show up in resultpage.asp. Should I open again the connection and retreive the CID? Please help.

Link to comment
Share on other sites

Directly after you insert everything into the database (as in the statement right after the SQL query), you need to get the highest ID number:

sqlcon.open("SELECT MAX(id) AS id FROM table");new_id = sqlcon.fields.item("id").value;...response.redirect("processpage.asp?id=" + new_id);processpage.asp:id = request.querystring("id");

Link to comment
Share on other sites

Directly after you insert everything into the database (as in the statement right after the SQL query), you need to get the highest ID number:
sqlcon.open("SELECT MAX(id) AS id FROM table");new_id = sqlcon.fields.item("id").value;...response.redirect("processpage.asp?id=" + new_id);processpage.asp:id = request.querystring("id");

I just want to make sure if I am doing it right: here is my modified code:
Form.aspID field as hiddenother formfields....Submit button------result.aspformfields = request.form("formfields")Set con = server.createobject("ADODB.Connection")con.open "dsn=mydsn"Set rs = server.createobject("ADODB.Recordset")rs.open "select * from tablename where 0=1",3,3addnew method to insert to databasers.updatecon.open("Select Max(id) as myid from tablename");new_id = con.fields.item("id").value [COLOR=red]'I dont understand this line...what is con.fields.item...is the fieldname from the table...[/COLOR]connection.closerecordset.closeresponse.redirect("resultpage.asp?id=" + new_id);-------resultpage.aspCID = Request.querystring("CID")response.write("formfields")Print command to print.

Thank you for your help.

Link to comment
Share on other sites

First, in form.asp you don't need the hidden ID field. You haven't created the ID yet, so there's no ID to pass.Why are you doing this?

rs.open "select * from tablename where 0=1",3,3

con.open("Select Max(id) as myid from tablename");new_id = con.fields.item("id").value

The name from item (here "id") needs to be the same name that you are getting from the database. You called the item from the database "myid", so you need to use con.fields.item("myid").value to read it.

response.redirect("resultpage.asp?id=" + new_id);...CID = Request.querystring("CID")

These two names also need to be the same. You are redirecting to resultpage.asp and passing a variable called id to it, but then you check for a variable called CID. Either do this:response.redirect("resultpage.asp?cid=" + new_id);or this:cid = request.querystring("id");But they both need the same name.

Link to comment
Share on other sites

I am still having problems with my code. I am getting the following error:Microsoft OLE DB Provider for ODBC Drivers error '80040e10' [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1. I have few questions:Why do you have use :

con.open "Select Max(ID) as myid from cform"

should it be :

rs.open "Select Max(ID) as myid from cform", con, 3, 3

because I am already opening the con.open "dsn=candidate"the error points at:

new_id = con.fields.item("myid").value

after the above code, giving rs.addnew.....so on...

Link to comment
Share on other sites

I am having problem with this code. When I use as this:

rs.open "Select Max(ID) as myid from cform", con, 3, 3

It is not recognizing the other fields in the database and I am getting an error such as "Item not to be found for the requested........."So, I changed the Select statement using all the fields such as:

"Select Max(CID), Cssn, ......from cform"

Now, I am getting this error message:Microsoft OLE DB Provider for ODBC Drivers error '80040e14' [Microsoft][ODBC Microsoft Access Driver] You tried to execute a query that does not include the specified expression 'Cssn' as part of an aggregate function. I understand since we are using the Max function, it is showing up that error. What is the round about to do this? Thanks for the help.

Link to comment
Share on other sites

I don't really like the way ASP interacts with databases, but this is how I do it:

var week_con = Server.CreateObject("ADODB.recordset");week_con.ActiveConnection = ts_dbcon_str;week_con.open("SELECT * FROM week WHERE startDate=" + sDate.valueOf() + " AND endDate=" + eDate.valueOf());if (!week_con.eof){  ...}week_con.close();

I only use the recordset object, not a connection (despite the name). I'm not sure what code you are working with now or what problems you are having. If you want help paste the code you are using. For the SQL statement, just use one query to get the new ID, and a second query to get all of the information about that ID.

Link to comment
Share on other sites

I don't really like the way ASP interacts with databases, but this is how I do it:
var week_con = Server.CreateObject("ADODB.recordset");week_con.ActiveConnection = ts_dbcon_str;week_con.open("SELECT * FROM week WHERE startDate=" + sDate.valueOf() + " AND endDate=" + eDate.valueOf());if (!week_con.eof){  ...}week_con.close();

I only use the recordset object, not a connection (despite the name).  I'm not sure what code you are working with now or what problems you are having.  If you want help paste the code you are using.  For the SQL statement, just use one query to get the new ID, and a second query to get all of the information about that ID.

Sorry and thank you for your help. I didnt understand in the first place regarding the connection and recordset object. I am working on the code to refresh the page without getting the data to the database. And your suggestion seemed good and so I started working in your direction. Now, it seems good but getting error when using the sql statement. Here is my code:result.asp
<%Set con = Server.createobject("Adodb.Connection")con.open"dsn=Candidate"Set rs = Server.createobject("Adodb.Recordset")Set rs = con.execute ("Select   Max(CID) from Cform")rs.AddNewrs("Cssn") = Cssnrs("Cjobno") =  Cjobnors.updatenew_id = rs.fields.item("CID").value%><%rs.closeSet rs=nothingcon.closeSet con=nothing%>response.redirect("resultpage.asp?Cid=" + new_id)

So far so good. But I am not able to get to the sql statement your way. Any help would be greatful. Thanks again.

Link to comment
Share on other sites

I'm not real sure what you're doing with the recordset object. I think it's just easiest to use SQL statements, instead of using the methods of the recordset object to deal with the database. So just use queries to do everything:

Set rs = Server.createobject("Adodb.Recordset")rs.ActiveConnection = your_connection_stringrs.open "INSERT INTO Cform ..."rs.open "SELECT MAX(CID) as CID FROM Cform"new_id = rs.fields.item("CID").valuers.close

Link to comment
Share on other sites

I'm not real sure what you're doing with the recordset object.  I think it's just easiest to use SQL statements, instead of using the methods of the recordset object to deal with the database.  So just use queries to do everything:
Set rs = Server.createobject("Adodb.Recordset")rs.ActiveConnection = your_connection_stringrs.open "INSERT INTO Cform ..."rs.open "SELECT MAX(CID) as CID FROM Cform"new_id = rs.fields.item("CID").valuers.close

I fixed the sql issue now I have problem passing the value of New_id to the next page, that is resultpage.asp. For some reason, the value is not passing. When I look at my browser URL it shows as:http://mydomain/resultpage.asp?ID=Code for the above:
rs.addnewrs.field1rs.field2rs.updatenew_id = rs.fields.item("ID").value%><%rs.closeSet rs=nothingcon.closeSet con=nothing%>br /><br /><%response.redirect("resultpage.asp?ID=" & new_id)

on the resultpage.asp:

ID = request.querystring("new_id")

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...