Forum Moderators: open

Message Too Old, No Replies

Key activated multiple submit buttons

         

salewit

1:43 am on Sep 17, 2009 (gmt 0)

10+ Year Member



This is a home-made cash register we use for trade shows. I've got ONE form and 5 Submit buttons. Everything works great, but it's gotten cumbersome during busy times to fumble with the mouse to click on the submit buttons. I've used the key equivalents as well, but even the multiple key presses (ALT-U, etc) are a pain.

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?

Fotiman

12:59 pm on Sep 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your PHP code is looking for the existence of one of the submit button name/value pairs in the POST, so you could do something like this:

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();
}
}

You could probably abstract out the functionality that creates the element, passing in the name of the input.

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.

astupidname

2:47 pm on Sep 17, 2009 (gmt 0)

10+ Year Member



You could go this route too, rather than multiple submit buttons, use radio buttons to select a "submission type" of sorts, and a single submit button:

<!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>

salewit

4:47 pm on Sep 17, 2009 (gmt 0)

10+ Year Member



Thank you both for your help. I really appreciate it. One of the reasons I can't use normal characters like C, U and S is because we've got a barcode reader connected. And the barcode reader just mimics the keyboard. All of our products have alpha-numeric barcodes, so scanning items would easily trigger the alpha keypresses. I could use [ or ] or something like that, but I just thought it would be easier to find a function key not being used and use that.

astupidname

1:15 pm on Sep 18, 2009 (gmt 0)

10+ Year Member



so scanning items would easily trigger the alpha keypresses.

So, apparently you still have a problem then, and if I'm not missing something here, perhaps all you need is a manner of enabling the keypress functionality after the barcode has been scanned. For instance in the following alteration of my previous posted code, the F9 key can be used to enable/disable the functionality -which I presume (not knowing much about integrating barcode scanners) you could do after the barcode has been scanned. So the following would require 3 key presses to submit (one (F9) to enable selecting submission type and submitting via keys, one (c,u,or s) to select submission type, and one (F2) to submit), at least you wouldn't have to use key combination presses or 'fumble' with the mouse:

<!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?

salewit

3:51 pm on Sep 18, 2009 (gmt 0)

10+ Year Member



No I think you understand it perfectly. And although it would have been best to use the C,U and S keys, the example using the function keys is installed and working perfectly. I may give your example a shot to see which is easier to deal with.

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!