Forum Moderators: open
I know ZERO Javascript, but I pieced something together that works for ONE button. This is what I've got:
<?
# Detect which submit was pressed
if (isset($_POST['submit_cancel']))
if (isset($_POST['submit_save']))
if (isset($_POST['submit_update']))
?>
<html>...
<script language="Javascript">
function submitkey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode == "113") { # F2 Key
document.formname.submit();
}
if (charCode == "114") { # F3 Key
document.formname.submit();
}}
</script>
</head>
<body OnLoad="document.formname.item_number_prompt.focus();"> <!-- Keeps cursor on input field -->
<form name="formname" method="post">
...
<input type="text" name="item_number_prompt" onKeyDown="javascript:return submitkey(event);">
...
<input type="submit" name="submit_cancel" value="Cancel" accesskey="C">
<input type="submit" name="submit_update" value="Update" accesskey="U">
<input type="submit" name="submit_save" value="Save" accesskey="S">
How would I go about passing something so my PHP can pick it up on the processing end which key was pressed?
function submitkey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
var el;
switch (charCode) {
case 113:
el = document.createElement('input');
el.type = 'hidden';
el.name = 'submit_cancel';
document.formname.appendChild(el);
document.formname.submit();
case 114:
el = document.createElement('input');
el.type = 'hidden';
el.name = 'submit_update';
document.formname.appendChild(el);
document.formname.submit();
}
}
Also, don't use the "language" attribute in your script tag. It's invalid. Instead, your script tag should look like this:
<script type="text/javascript">
Also, if you're going to use inline event handlers, it's better to use all lowercase, as in "onload" instead of "OnLoad" and "onkeydown" instead of "onKeyDown".
Hope this helps.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Some Title</title>
<script type="text/javascript">document.onkeydown = function (evt) {
var charCode = (evt && evt.which) ? evt.which : window.event.keyCode;
var codes = {"67":"cancel", "85":"update", "83":"save"}; //67 is keyCode for "c", 85 is "u", 83 is "s"
if (''+charCode in codes) { //a key: "c" or "u" or "s" was pressed:
this.getElementById(codes[charCode]).checked = true;
} else if (charCode == 113) { //if F2 was pressed:
this.forms["formname"].submit();
}
}
</script>
</head>
<body>
<form name="formname" method="post" action="">
<table>
<tr><td>Cancel (c)</td><td><input type="radio" name="submit_type" id="cancel" value="Cancel"></td></tr>
<tr><td>Update (u)</td><td><input type="radio" name="submit_type" id="update" value="Update"></td></tr>
<tr><td>Save (s)</td><td><input type="radio" name="submit_type" id="save" value="Save"></td></tr>
</table>
<input type="submit" value="Submit (F2)">
</form>
<?php
$msg = "The submission type was: ";
$submit_types_arr = array("Cancel" => $msg.'cancel', "Update" => $msg.'update', "Save" => $msg.'save');
if ($submitType = $_POST["submit_type"]) {
echo "<p>$submitType was selected!</p>";
if ($submit_types_arr[$submitType]) {
echo "<p>{$submit_types_arr[$submitType]}</p>";
} else {
echo "<p>Not in submit_types_arr!</p>";
}
} else {
echo "<p>no radio button selected!</p>";
}
?>
</body>
</html>
so scanning items would easily trigger the alpha keypresses.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Some Title</title>
<script type="text/javascript">function setEnabled(getStatus) {
var statusInput = document.getElementById("selectionEnabledStatus");
var currTxt = statusInput.value;
var isDisabled = /disabled/i.test(currTxt);
if (getStatus) {
return isDisabled;
}
statusInput.value = (isDisabled) ? currTxt.replace('Disabled','Enabled') : currTxt.replace('Enabled','Disabled');
statusInput.style.backgroundColor = (isDisabled) ? 'green' : 'red';
return isDisabled; //returns true if textual status changed to enabled, false if textual status changed to disabled
}document.onkeydown = function (evt) {
var charCode = (evt && evt.which) ? evt.which : window.event.keyCode;
var codes = {"67":"cancel", "85":"update", "83":"save"}; //67 is keyCode for "c", 85 is "u", 83 is "s"
var isDisabled = setEnabled(1);
if (charCode == 120) {
isDisabled = setEnabled();
} else if (''+charCode in codes && !isDisabled) { //a key: "c" or "u" or "s" was pressed:
this.getElementById(codes[charCode]).checked = true;
} else if (charCode == 113 && !isDisabled) { //if F2 was pressed:
this.forms["formname"].submit();
}
}
</script>
</head>
<body>
<form name="formname" method="post" action="">
<h4>Must press F9 to enable/disable selecting submission type and submitting via keys</h4>
<p><input type="button" id="selectionEnabledStatus" size="35" style="color:white; background-color:red;" onclick="setEnabled();" value="Key Selecting And Submitting Disabled"></p>
<table>
<tr><td>Cancel (c)</td><td><input type="radio" name="submit_type" id="cancel" value="Cancel"></td></tr>
<tr><td>Update (u)</td><td><input type="radio" name="submit_type" id="update" value="Update"></td></tr>
<tr><td>Save (s)</td><td><input type="radio" name="submit_type" id="save" value="Save"></td></tr>
</table>
<input type="submit" value="Submit (F2)">
</form>
<?php
$msg = "The submission type was: ";
$submit_types_arr = array("Cancel" => $msg.'cancel', "Update" => $msg.'update', "Save" => $msg.'save');
if ($submitType = $_POST["submit_type"]) {
echo "<p>$submitType was selected!</p>";
if ($submit_types_arr[$submitType]) {
echo "<p>{$submit_types_arr[$submitType]}</p>";
} else {
echo "<p>Not in submit_types_arr!</p>";
}
} else {
echo "<p>no radio button selected!</p>";
}
?>
</body>
</html>
Does that get you any further at all or am I not understanding the problem correctly?
FWIW, barcode readers are exactly like keystrokes. If you type say "abc123" in Wordpad, then change the font to say one of the public domain barcode fonts. Then you take the scanner and scan that barcode, it's as if you typed "abc123" really quickly. Kind of neat actually. It's such old technology now, but still fascinates me!