Jump to content

[SOLVED] PHP with Javascript


Doyley

Recommended Posts

Hi all,

 

I am trying to build a script where when a user closes a window it will update a field in the database.

 

Something similar to the following but the following does not work...

 


<script language="Javascript">

function onClose{

<?php

$update=mysql_query("update table set column='1' where uid='$uid'");

?>

}

</script>

<body onUnload="onClose();">
Hello World!
</body>

 

Does anybody have any ideas?

 

Thanks!

Link to comment
Share on other sites

Its impossible to do reliably (in that it will not work 100% of the time), but if you are deadset on doing it even if its not reliable, then you need to use AJAX to do it. On unload, the script is executed, sending a request to a php script that updates the database. But anytime someone has javascript turned off it will fail, screwing up your numbers to some degree.

Link to comment
Share on other sites

You have 2 options.

1) Use ajax to post an event to a php script which will perform the query you're after.

2) onClose redirect to a temporary page which will update the record.

 

On both accounts i'm unsure if the event will actually get triggered by browsers when closing.

Link to comment
Share on other sites

OK thanks guys.  I will try them.

 

I will explain what I am trying to do.

 

I am building some software for a call centre.  They will have a list of all clients that they need to call.  When they click on the name of a client a new window will open with all of their details but "lock" that record so that no other staff can access the record and cause problems.  But when they close that window the record needs to be "unlocked".

 

The Javascript disabled thing shouldn't be a problem, I will just have a word with the IT guys to make sure they are all enabled.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.