Submit a PHP Form with any of keyboard shortcut key - javascript

i wants to submit a php form with any of function key or a Any of keyboard shortcut key.
I try following code. In that i do operation using keyboard keycode. but its not submits the form.
Press "TAB" button from Keyboard
document.onkeydown=function(evt){
var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;
if(keyCode == 9)
{
//your function call here
document.test.submit();
alert("Key Pressed");
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<?php
if(isset($_POST['search']))
{
echo $search = $_POST['search'];
}
?>
<body>
<form name="test" action="#" method="POST">
<input type="text" name="search" />
</form>
</body>
I wants to submit a PHP form with any of Function key or Any of combinations or shortcut key.
So How can I do it with JQUERY,JS,AJAX OR PHP.

I founded a JS to Allow Form Submit using keyboard key combination.
It quite easy.
See Code
shortcut.add("Ctrl+B",function() {
document.getElementById("sbmt").click();
alert("Form Submitted...");
},{
'type':'keydown',
'propagate':true,
'target':document
});
When Press Ctrl + B Form Was Submits.
This is Link to JS
openjs To Allow Shortucut

Checkout this if you want to submit on click of any function keys
JsFiddle
$(document).keyup(function(e) {
if(e.which <124 && e.which >111){
alert('function keys');
//submit your form here
}
});

try this it will work.
document.onkeydown=function(evt){
var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;
if(keyCode == 9)
{
//your function call here
document.forms["test"].submit();
alert("Key Pressed");
}
}
Check this code in your local not on third party. This will work perfect on localhost. this is work for me.

Related

Click to behave like a Return

I'm building a form and i'd like to make my radio button behave like i've pressed Enter
something like onclick="keycode=13"
any idea how to do ?
If you wanna submit the form, you can just attach the click handler to submit the form.
<input type="radio" onclick="this.form.submit();" />
Or you an even trigger the enter key!
<input type="radio" onclick='e = jQuery.Event("keypress"); e.which = 13; e.keyCode = 13; this.trigger(e);' />
In your event handler you can submit a form using the following code:
document.getElementById("myForm").submit();
Add onkeypress on your form:
<form .. onkeypress="return myEvent(event)">
<input type="radio" id="myRadio">
Than add Javascript
<script type="text/javascript">
function myEvent(e) {
e = e || window.event;
var key = e.keyCode || e.charCode;
if (key === 13) {
// enter down
document.getElementById('myRadio').checked = true;
return false; // prevent form post on enter
}
return true;
}
</script>
I think the code above should work, because I have very similar code to prevent form post on Enter press.
Bit late, however...
<form name="myform">
<input type=radio onclick="this.parentNode.submit()">click</input>
</form>
i managed to achieve this by adding the followind code to the script
document.addEventListener( 'click', function( ev ) {
ev.preventDefault();
self._nextQuestion();
} );

Why isn't the value being printed?

Here's the short code:
<!DOCTYPE html>
<script type="text/javascript">
function myKeyPress(e)
{
var keynum;
var list;
if(window.event)
{
keynum = e.keyCode;
list = keynum;
if(list == 115)
{
alert("You pressed the 'S' key.");
}
}
}
</script>
<form>
<input type="text" onkeypress="myKeyPress(event);"/>
</form>
When I hit a key, myKeyPress should be called and the event should be passed to the function. In the function body, e (the event parameter) should fetch the Keynum and obtain the input value. That value should be 115 if I were to press 'S' on the keyboard. List should then have the value of Keynum, and list is checked to see if its value is equal to 115 (it should be). If so, it should alert the corresponding text in a message box on the screen. It doesn't do it though. Why?
Here is a shortened version of your code:
function myKeyPress(e) {
var keynum = e.which ? e.which : e.keyCode;
alert(keynum); // Just to see all key presses, remove when done :)
if (keynum == 115) {
alert("You pressed the 'S' key.");
}
}
I am binding it using JavaScript not using an HTML attribute:
document.getElementById('example').onkeypress = myKeyPress;
So I added an ID:
<input type="text" id="example" />
I find that this works. I'm not sure why you were testing window.event in your code?
s key is nr 83 and not 115.
you could find out like this with jQuery.
<script type="text/javascript">
$('#myinput').on('keyup',function(e){
console.log(e.keyCode);
});
</script>
<input type="text" id="myinput" />

Press enter to submit form if checkbox is checked

I have a form that submits with a button, but I want to also allow to submit by typing the ENTER key if a checkmark has been checked.
I've got the enter part working:
<form action="index.php">
<textarea onkeydown="pressed(event)"></textarea>
</form>
<input type="checkbox" id="enterCheckbox" />
<script>
function pressed(e) {
if ( (window.event ? event.keyCode : e.which) == 13) { document.forms[0].submit() }
}
</script>
Now I want to only run that function if #enterCheckbox has been checked, but I'm having trouble doing it correctly. Any help?
Does what you have work as expected other than just adding the extra condition? If so, you should be able to just add in the extra check with && in your current if(...) check.
Something along the lines of $('#enterCheckbox').attr('checked') should tell you if the checkbox is checked or not.
You should be able to do something like this:
if(document.getElementById(enterCheckbox).checked)) {
// do all the good stuff
}
Your function should include an extra condition.. something like:
<script>
function pressed(e) {
if ( ( (window.event ? event.keyCode : e.which) == 13) AND (document.getElementById('enterCheckbox').checked) ){ document.forms[0].submit() }
}
</script>
If using jQuery, I suggest using selectors to make life easier. But what you want for checking if #enterCheckbox is checked is .is(':checked').
Updated HTML
<form id="myForm" action="index.php">
<textarea id="myTextarea"></textarea>
</form>
<input type="checkbox" id="enterCheckbox" />
jQuery Implementation
$('#myTextarea').on('keydown', function(e) {
var key = window.event.keyCode || e.which;
if($('#enterCheckbox').is(':checked') && key == 13) {
$('#myForm').submit();
}
});
To check if the box is checked, using jQuery:
$("#enterCheckbox").is(":checked")
So your <script> will be:
function pressed(e) {
if (($("#enterCheckbox").is(":checked")) && ((window.event ? event.keyCode : e.which) == 13)) {
document.forms[0].submit()
}
}

How can i change the textbox value on pressing space?

I want to change the English words into Hindi
every time I press space in textbox its a keypress event but how to apply only for space key.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#textboxid').keypress(function(e){
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 32) { //space keycode
//Do language transilation here
}
});
});
</script>
<form id="form1" name="form1">
<input type="text" name="textfield" id="textboxid" />
</form>
Space is 32. So using jQuery keypress event you can do it with:
$("textarea").on("keypress", function(e) {
if (e.keyCode == 32) {
// ...
}
});
For a vanilla js answer:
document.getElementById( 'textboxid' ).onkeypress = function( e ) {
if ( e.keyCode === 32 ) {
// Translate into Hindi
}
}

Execute function by pressing "return" using jQuery?

i have two text inputs like the following, i don't want to use <form> , so i want when people press "return" BUTTON after filling the inputs, a function called "doit()" should be executed.
<script>
function doit(){
alert("you submitted the info");
..........ajax code........
}
</script>
<input type="text" id="email" />
<input type="text" id="skills" />
Thanks
The following will do what you are looking for.
$('#emails, #skills').keypress(function(e){
if( e.keyCode == $.keyCode.ENTER || e.keyCode == $.keyCode.NUMPAD_ENTER ){
yourSubroutine();
}
});
$('#name, #last').bind('keydown', function(e) {
if (e.keyCode == 13 || e.keyCode == 108) {
doit();
}
});
KeyCode can be found here
Check out this question. Essentially, you'll want to bind a keypress listener, and then check for the return key.

Categories