-1

I did a small table thats ask for product name and product # my question how can I alert the user that he is leaving the page without saving the information.

<form action="index.php" method="post">
<input type="text" name="pnumber" value="" placeholder="Product Number...."/> 
<input type="text" name="pname"   placeholder="Product Name...." /> <br>
<button id="save">save</button>
</form>
Jan Leon
  • 176
  • 1
  • 14
  • http://www.bitnative.com/2013/08/19/warn-users-of-unsaved-changes-with-jquery/ has some very good ideas for this. – Lee S May 22 '14 at 00:16

2 Answers2

1

Modern browsers support the window.onbeforeunload event which you can trap to offer this kind of functionality.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
1

One approach: checking if the fields are empty

$(window).unload(function() {
    var pnumber = $("input[name=pnumber]").val();
    var pname = $("input[name=pname]").val();



  if(pnumber==="" || pname==="")
   {
     alert("Please fill the form");
     return false;   
   }
});

Another : have a global flag to chec

var saved = false; // global variable

$("#save").click(function() {
   saved = true;
});

$(window).unload(function() {
    if(saved==false)
   {
     alert("Please fill the form");   
     return false;
   } 
});
mohamedrias
  • 18,326
  • 2
  • 38
  • 47