Link to home
Start Free TrialLog in
Avatar of hibbsusan
hibbsusan

asked on

ajax write form data to file javascript confirm box

I have a simple confirm button

var leave=confirm("are you sure you want to leave? Information you have entered will be lost!");
if (leave==true)
  {
return true;
  }
 else
  {
  return false;
  }
}

Open in new window


Is there anyway to use ajax (jquery+ajax is my only ajax experience) to write some data to a file if the user decides to leave the page? That way I can collect his or her name, IP, &c. so I have a confirmation who has cancelled the order mid-way through. I was thinking about collecting the data in the form fields with jquery before I wrote to the file, if that makes any difference.

Thanks!!
Avatar of hibbsusan
hibbsusan

ASKER

and by write data to a file, i mean, create a txt file on my server (/incomplete_order_logs/ or something), and write to that file.

pardon me: that was pretty unclear the first time.

Thanks!
I presume you already have the timing of the Confirmation Dialogue worked out, etc. so that it fires appropriately.  Also, I presume you already have the information in session that you want to write.

If so, before you "return true", fire the AJAX event that passes the information to your server-side page that writes the file.
I presume you already have the timing of the Confirmation Dialogue worked out,
yes.

Also, I presume you already have the information in session that you want to write.
yes.

If so, before you "return true", fire the AJAX event that passes the information to your server-side page that writes the file.
This is exactly what I want to do, but I haven't got any idea what the ajax code should be!

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe 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
when i add this to the window.onbeforeunload event, it pops up the confirm box both when the pages loads and unloads.. any idea why?
and chrome blocks alerts and confirms. Any way around that?? the code below works in safari, but not chrome..


$(window).unload(function() {
  var leave=confirm("are you sure you want to leave? Information you have entered will be lost!");
if (leave==true)
  {
return true;
  }
 else
  {
  return false;
  }

});

Open in new window