Unsaved Changes On Forms

If you try to abandon a form, you’ll be reminded that you’ve made changes. In addition, a pop up message will come up, asking you whether to stay on the page or to abandon it. This feature is made possible using a Javascript function. This short article will show how to alert users if they made changes to a form, prior to canceling or going to another link.

The Form

In this example, I created a simple form with one input called “username.” I’ve added “onchange=’needToConfirm=True'” to monitor the input field. I also added a save and cancel button, as well as an internal link for testing the form later. The save button calls the “needToConfirm” function when the save button is clicked. The cancel button simply reloads the same page when the cancel button is clicked. Here’s the HTML markup.

<form id="login" method="post" action="">
<input type="text" name="username" size="40" value="" onchange="needToConfirm=true" /><br>
<input type="Submit" name="save" value="Save" onclick="needToConfirm=false;" />
<input type="button" value="Cancel" onclick="window.location='confirm.html'" />
<a href="confirm.html">Link</a>

Javascript

The unsaved changes feature is made possible using Javascript. The function starts out setting needToConfirm to false. A pop up window comes up notifying the user that they have unsaved changes on the form. The user is prompted whether to stay on the page or to leave the page. Add the following Javascript code at the end of the form. Go ahead, test the demo below.

<script>
needToConfirm = false;
window.onbeforeunload = askConfirm;
function askConfirm() 
{
 if (needToConfirm) 
 {
 return "You have unsaved changes.";
 }       
}
</script>

Demo