Jump to content

updae a database value when the browser is closed

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
welton122

welton122

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
hi all,

ok basically i need to run a line of MySQL code when the browser is ran to update a value in the database. I have found some forums which come up with solutions but they are in languages which i don't know e.g. ASP.

Is there a way of doing this in either PHP or JavaScript?

Thanks,
J

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Can you explain what you are wanting to do, further?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Sounds like something you would want to happen on session end to mark in the database that a user is no longer active.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
welton122

welton122

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
WingedPanther has described exactly what i am after, does anyone know a way of accomplishing this? i heard rumors of using AJAX but im not sure?

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Yes, you could use Ajax to tell the database to set them offline when they close the window/tab. A simple JS method to perform this on most browsers is the following.
<html>
<title></title>
<head>
<script language="Javascript">
function doAjax()
{
   alert('Example: You had exited');
}
</script>
</head>
<h1>Try to exit the browser.</h1>
<body onUnload="doAjax();">
</body>
</html>
Of course replacing the alert() with the Ajax call to the PHP script of which will mark them offline.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
welton122

welton122

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
Thanks I believe that's what was looking for. Does anyone know the Ajax code to call a php page?

Thanks

#7
welton122

welton122

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
hi,

okay basically i need a PHP script to run when a session ends. The script will update a value in the database. This script needs to be ran even if the user has closed the browser. Is there a way of doing this?

Many Thanks,
Jake

#8
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts
I would like to reffer you to another thread, with a similar request. Please view this :

http://forum.codecal...html#post282238

:D You should rep+ me so that I can win :D

My Blog | Ask me!
Error : Satan did it

#9
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
I had merged your threads, please do not create more than one thread on the same subject. It leaves out a lot of information of the previous one.

Your question about AJAX can be answered easily with a tutorial, as there is no special function required to be used with the example I had given before:
Ajax Tutorial - Form

Instead of my alert() , you will replace that simply with the javascript required to start the AJAX request. When you do that you will need to create a PHP page that accepts a request to update the database value, and tell AJAX to call 'updatedb.php?user=123&value=xxx' for example, updatedb.php handling those parameters.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.