tags:

views:

74

answers:

1

hi friends,

I'm developing a website in php.

I want to show a message something like javascript alert, when a user tries to edit or add something in a form and tries to navigate to some other section without saving the modification, i want to show a message to them,

that you are about to navigate about from this page, your modifications are not saved, do you want to continue?

how can i do this??

any one have an idea ???please share it with me..

Thanks

+2  A: 

Use the beforeunload event. Pseudo-code:

window.onbeforeunload = function(e){
   e = e || window.event;
   // check if the user has edited sth
   if(userHasEditedSomething()){
      var msg = "You have unsaved changed. Do you want to navigate away from this page?";
      e.returnValue = msg;
      return msg;
   }
}

Your job is to implement the userHasEditedSomething function that will return true when the user has unsaved changes (filled-in form fields) and false otherwise.

most of the browsers support this event

Rafael
what is thisif(userHasEditedSomething()what is userHasEditedSomething() ????
tibin mathew
as I mentioned in my answer, the code is pseudo-code and the userHasEditedSomething is a function that you have to implement, that will return true if the user has unsaved changes, false otherwise. Of course, you can skip the whole if and display the message everytime the user tries to unload the page.
Rafael
sorry for not mentioning this in my answer
Rafael
i have implemented this in my site, and its working.but there is another problem, that alert is appearing eventhough i clicked in my save button.how can i solve that
tibin mathew
If that is the case, your implementation of userHasEditedSomething() is probably incorrect.
berty
you have to handle form saving on your page, because sending forms is the same as navigating away from page. You can solve this in many ways, for example by removing the beforeunload event-handler while submitting form: document.getElementById('form_id').addEventListener('submit', function(){window.onbeforeunload = null;}, false)
Rafael