I'm trying to submit forms programatically when the user leaves the page, regardless of whether or not they've clicked on the 'Submit' button. I've used this method in the past to submit forms from within a function, but for some reason I can't get it to work with onclick
.
The form is being submitted properly when the user clicks on the 'Submit' button, so the html form and php are definitely set up correctly. I'm using jquery-1.8.2
<form name="form2" id="form2" method="post" action="form2-exec.php">
<!-- Form Elements -->
<a class="back" href="form1.php" onClick="submitForm()">BACK</a>
<a href="#" class="next"><input type="submit" value="SUBMIT" /></a>
</form>
<script language="javascript" type="text/javascript">
function submitForm() {
$("#form2").submit();
}
</script>
I've also tried using a click function
$(".submitForm").click(function () {
$("#taste").submit();
});
And replacing this line in the HTML:
<a class="back" href="form1.php" onClick="submitForm()">BACK</a>
With:
<a class="back submitForm" href="form1.php">BACK</a>