Link to home
Start Free TrialLog in
Avatar of payami
payami

asked on

I want to execute some sql statement in onunload event of a page

I want to call a server side function(a function in my asp code)
from <body> tag in onunload event.
I want to execute some sql statement in a function.
<BODY bgcolor=#ffce9c onunload="FunctionName()">
it does'nt work.
How can I call a server side function from some client side scripts?
Is it Possible?
Avatar of bvinson
bvinson
Flag of United States of America image

No can do.  The onload event is called when the browser loads the page, which means it has already had all the ASP (server-side) processing done to it.
If you bump the function over to javascript or even vbscript, it will process on the client-side and should fire you function off onload.

What are you trying to accomplish?  We may be able to come up with a way to do it.
bvinson
ASKER CERTIFIED SOLUTION
Avatar of Wakie
Wakie
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, I missed the UN in onUNload...but you still must execute the function through a scripting language (or through Sessions, but that is very unreliable)...

Wakie - I've never used js to fire a SQL event, is the syntax similar to ASP (i.e. does it use standard SQL statements) or is it some other process?

Thanks payami for the question and Wakie for a little extra knowledge...

bvinson
Exactly the same, the Javascript function merely redirects the script and sends a value through the query string :-)
Avatar of payami
payami

ASKER

dear bvinson & wakie,
thank you for your help.I still have problem and I little confused
With wakie's code because of my little knowledge in jscript.
I explain my target so maybe you can Solve my problem:
I have some temprory data in a table(in a mdb file) and I want to delete
them when user close the browser.it means I have to execute somthing like this:
strSQL="delete * from tTemp"
I declare a function contains my sql statment and con.execute then
call it from the onunload event.
maybe it has a better way than calling a function or sub if it has one
please let me know.
thank you.
Hi payami,

If that's the case perhaps you would be better off storing your data in an application or session variable?

Then, you won't need to worry about destroying the data as this is done automatically.

Regards,
Wakie.
Avatar of payami

ASKER

Hi wakie,
my data is binary.I mean they are pictures that I storing
them in a two tables.One main table for saving all pics after user confirmation(save button clickig)and another temprory table for befor user confirmation.so I do many proccessing on them and it seems imposible to change all of them.I realy not have any other way?I treid global.asa
and session_onend but I saw that mistake again.
Thank you for your attention.
 
Here's another idea:

Why don't you store the PATH of the image in the database and store the image in a directory on your web server?
Avatar of payami

ASKER

dear wakie,
I review your example for many time and I think I undrestand it.it seems very useful for me.
I used it in my code and fetched my function to it.
I do not recive any errors but nothing goes happen.
Is onUnload event fire when user Close the Browser?
Is anything else that I have to Set.
I hope I dont disturbe you very much.
thanks,
payami
There's no reliable way to tell if the user has closed their browser.

Another idea is perhaps to use a global.asa file and give them some Session variable, and delete their information from the database when their session times out (which is by default 20 minutes)... see here:

http://www.w3schools.com/asp/asp_globalasa.asp

Hi payami,

Once you have set the path to your images in your database, you then create a connection to your database and retreive the image paths, similar to this:

<%
'Create database connection

SQL = "SELECT * FROM Images"
Set RS = DB.Execute(SQL)

Do Until RS.EOF
   Response.Write "<img src=""" & RS("ImagePath") & """>"
   RS.MoveNext
Loop
%>

Regards,
Wakie.
Avatar of payami

ASKER

Dear Wakie,
Thank you for all your great solutins.they are realy
useful and efficient.
Payami.
You're welcome, payami.

Thanks,
Wakie.