I have a form that I use in my CMS that I would like to add the extra open to save the form on keypress: "Ctrl + S"
This works for all inputs apart from the submit buttons are not being sent, this simple example shows what I mean:
<?php
if(isset($_POST['save'])){
die('save= ' . $_POST['save']);
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
html { height: 100%; }
body {
color: #262626;
background: #f4f4f4;
font: normal 12px/18px Verdana, sans-serif;
height: 100%;
}
#container {
width: 760px;
margin: 0 auto;
padding: 10px 60px;
border: solid 1px #cbcbcb;
background: #fafafa;
-moz-box-shadow: 0px 0px 10px #cbcbcb;
-webkit-box-shadow: 0px 0px 10px #cbcbcb;
min-height: 100%;
height: auto !important;
height: 100%;
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
(function($){
$(document).ready(function(){
// Save Form
$(window).keypress(function(event) {
if (!(event.which == 115 && event.ctrlKey) && !(event.which == 19)) return true;
$("#container form").submit();
event.preventDefault();
return false;
});
});
})(jQuery);
</script>
</head>
<body>
<div id="container">
<form action="" method="post">
<label for="">Name</label>
<input type="text=" name="name" value="" />
<input name="save" type="submit" value="Save" />
<input name="create" type="submit" value="Create" />
</form>
</div>
</body>
</html>
The value of a submit
button is only included in the request if it is the submit
button that was clicked on.
Because you're submitting the form directly (using JS), you're not clicking on a submit button, so none are been submitted.
Instead of calling .submit()
on the form, try calling .click()
on the submit button you want to be included.
$(window).keypress(function(event) {
if (!(event.which == 115 && event.ctrlKey) && !(event.which == 19)) return true;
$("#container form input[name=save]").click();
event.preventDefault();
return false;
});
Try:
if($("#container form :submit:first").length)
{
$("#container form :submit:first").click();
}
else
{
$("#container form").submit();
}
It will trigger a click on the first submit-button(if available)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With