title: How to repopulate form variables?
body: <p>I've got a page that should warn if the user tries to leave the page having unsaved data. This dialog means "You have unsaved data. Do you want to conitnue?"</p>
  
  <p><img src="https://i.stack.imgur.com/qyuXk.png" alt="enter image description here"></p>
  
  <p>The problem is that the form values go blank when the user clicks a link to move on and is presented with the dialog. I want to form values to stay in place so that the user actually can choose not to continue and then still have left what was entered to the form. </p>
  
  <p>The way the code is now is that it keeps a flag for whether the form has dirty data and that is a hidden variable:</p>
  
  <p><code>&lt;input type="hidden" name="saveStatus" value="&lt;%=saveStatus%&gt;" /&gt;</code></p>
  
  <p>Touching the form sets this hidden variables with <code>onclick</code>and <code>onkeypressed</code>. </p>
  
  <p>On the server I've got some logic that checks whether data is dirty:</p>
  
  <pre><code>public boolean returnToUnsavedData(ISessionHandler sessionHandler,
          Action action) {
      String saveStatus = sessionHandler.getRequestParameter("saveStatus");
  
      if (saveStatus != null &amp;&amp; !saveStatus.trim().equals("")) {
          setHasUnsavedData(true);
      } else {
          setHasUnsavedData(false);
      }
  
      if (hasUnsavedData()) {
          if (!"menuHasUnsavedDataOk".equalsIgnoreCase(action
                  .getActionCommand())
                  &amp;&amp; !action.getActionName().equals("Administrera")) {
              ResourceBundle messages = ResourceBundle.getBundle("messages");
              UserMessage um;
              if (action.getActionCommand().equals("fastsearch")) {
  
                  um = new ExtendedUserMessage(
                          messages.getString("PV24"), UserMessage.TYPE_WARNING,
                          "Varning", action.getActionName(),
                          action.getActionCommand(), sessionHandler.getRequest().getParameter("fastsearch"));
              } else {
               um = new ExtendedUserMessage(
                      messages.getString("PV24"), UserMessage.TYPE_WARNING,
                      "Varning", action.getActionName(),
                      action.getActionCommand());
              }
              action.addUserMessage(um);
              action.setReturnPage(action.getCurrPage());
              setLatestActionTarget(action.getActionTarget());
              return true;
          } else {
              setHasUnsavedData(false);
          }
      }
  
      return false;
  }
  </code></pre>
  
  <p>I suppose this is where I could check serverside for the variables and repopulate everything and then use a technique like above with the hidden variable to keep form values. </p>
  
  <p>Do you think that this idea will work or can you present alternative solutions?</p>