Jump to content

Recommended Posts

Can you give me an example or a good place to start? I have no java knowledge and am looking for a solution.

 

First, a terminology lesson.

 

Java != JavaScript.  At all.  They're not related.  So, no, JavaScript is not a subset of Java, or anything like that.  Java is an object-orientated language which runs on a virtual machine.  It was created by Sun Microsystems.  JavaScript is a browser-based scripting language originally developed by Netscape.  JavaScript 'borrowed' the 'Java' portion of the name as a marketing ploy.  No more, no less.  I'm not trying to jump down your throat or anything, but it's something that a lot of beginning developers get wrong, and is a pet peeve of mine.

 

To address your problem, you'll need to use AJAX.  AJAX is an acronym for Asynchronous JavaScript and XML.  The name is a bit misleading as you don't need XML to get it to work.  AJAX is necessary for this because of its asynchronous nature.  It allows you to send requests to the server, and obtain the results of those requests, without having to reload the page.

 

Since you probably don't want to learn the ins-and-outs of AJAX, you can probably get away with leaning heavily on the jQuery (documentation: http://docs.jquery.com/Main_Page - download: http://docs.jquery.com/Downloading_jQuery) library to help you with the syntax.

 

For this, you'll need a few things:

 

1. A callback function tied to the onclose event.

2. A flag to send to your PHP script to tell it to save the necessary data.

3. The PHP code that checks for the flag and saves the data.

 

You should do something like:

<script type="javascript" src="jquery-1.2.6.min.js"></script>
<script type="javascript">
   window.onclose = function()
   {
      $.post("yourScript.php", {toSave: "yes"}); //replace yourScript.php with the name of your script
   }
</script>

 

As you can see, no XML was used during the transfer of data.  Instead, a simple name-value pair was sent to the server as POST data.

 

And then, in your PHP:

if(isset($_POST['toSave']) && $_POST['toSave'] === "yes")
{
   //save your data to the db
}

 

Just an aside: You may have to use onunload instead of onclose.  I'm not sure if onclose works with all browsers.

Link to comment
https://forums.phpfreaks.com/topic/124314-window-help/#findComment-641993
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.