0

I have some code that gets the user to enter some information and I want that information to be submitted when the page is closed (just to be different) but at the moment, it only gets submitted if you say to stay on the page then exit the page. Thanks in advance

I have 2 files: form.html and and inputs.php

This is the code in "form.html":

<html>
<script>
  window.onclose = confirmExit;

  window.onbeforeunload = confirmExit;

  window.onunload = confirmExit;

  function confirmExit() {
    document.myform.submit();
    return false;
  }
</script>

<body>
  <form name="myform" action="welcome.php" method="post">

    Name: <input type="text" name="name" value="hi"><br> E-mail: <input type="text" name="email" value="test"><br>
    <input type="submit">
  </form>

  <button type="button" onClick="document.myform.submit()">Hello!</button>
</body>

</html>

And this is the code in "inputs.php":

<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>

</body>
</html>

Please can only help about the onunload etc be offered and not about any of the other code. Thanks

D.Drew
  • 1
  • You're not going to be able to force a user to do this. Browsers have heavily limited what you can do to an exiting user because of past abuse like this. – ceejayoz Apr 30 '17 at 11:33

1 Answers1

1

You should never force the user to do something that they haven't requested. Stick a submit button on it, don't force the user to complete it against their knowledge.

NGriffin
  • 71
  • 1
  • 7