Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prblm trying to emulate a Receipt Printer 2

Status
Not open for further replies.

dougcranston

Technical User
Oct 5, 2001
326
US
Trying to create a script to allow designated users to mock up a customer receipt. User problem is due to state laws, certain wording is required. So up till now, users submit text to our developer, he codes it, and then prints a copy and takes it back to the user. User has no concept of Receipt printer 40 chars normal type, 20 chars big type etc.

This results in alot of back in forth.

I volunteered to try and create a web page to allow the user to mockup the receipt so they can visually see a "representation" of what the receipt layout would look like. (Representation as the receipt printers builtin fonts are not Windows fonts so at best they would get the general look n feel.)

Users can have up to 15 lines of text.

I was trying to use a textarea box to represent the receipt tape.. I created the following test script with 3 inputs with plans to expand to the 15, but it doesn't work.

Only the last input box actually posts to the textarea.

I know I am doing something really stupid or overlooking the obvious, but I just can't see it.

Any suggestions would be greatly appreciated.. May be a different approach.

Thanks for your time and consideration.
Dougc

Sample code is as follows:

<%@ Language=Javascript %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<link rel="stylesheet" type="text/css" href="/tmp/tst.css">
<script>

function validchk() {

if(document.FORM1.strMaxLen1[1].checked==true) {
var length = '20';
document.FORM1.strText1.maxLength='20';
document.FORM1.strText1.className='lftNormal';
var chgLength = new String(document.FORM1.strText1.value);
document.FORM1.strText1.value=chgLength.substr(0,20);
document.FORM1.strSample.value=chgLength.substr(0,20) + "\r\n";
document.FORM1.strSample.className='ctrBig';
}
else {
var length = '40';
document.FORM1.strText1.maxLength='40';
document.FORM1.strText1.className='lftNormal';
var chgLength = new String(document.FORM1.strText1.value);
document.FORM1.strSample.value=chgLength + "\r\n";
document.FORM1.strSample.className='lftNormal';
//return true;
}


if(document.FORM1.strMaxLen2[1].checked==true) {
var length = '20';
document.FORM1.strText2.maxLength='20';
document.FORM1.strText2.className='lftNormal';
var chgLength = new String(document.FORM1.strText2.value);
document.FORM1.strText2.value=chgLength.substr(0,20);
document.FORM1.strSample.value=chgLength.substr(0,20) + "\r\n";
document.FORM1.strSample.className='ctrBig';
}
else {
var length = '40';
document.FORM1.strText2.maxLength='40';
document.FORM1.strText2.className='lftNormal';
var chgLength = new String(document.FORM1.strText2.value);
document.FORM1.strSample.value=chgLength + "\r\n";
document.FORM1.strSample.className='lftNormal';
//return true;
}

if(document.FORM1.strMaxLen3[1].checked==true) {
var length = '20';
document.FORM1.strText3.maxLength='20';
document.FORM1.strText3.className='lftNormal';
var chgLength = new String(document.FORM1.strText3.value);
document.FORM1.strText3.value=chgLength.substr(0,20);
document.FORM1.strSample.value=chgLength.substr(0,20) + "\r\n";
document.FORM1.strSample.className='ctrBig';
}
else {
var length = '40';
document.FORM1.strText3.maxLength='40';
document.FORM1.strText3.className='lftNormal';
var chgLength = new String(document.FORM1.strText3.value);
document.FORM1.strSample.value=chgLength + "\r\n";
document.FORM1.strSample.className='lftNormal';
//return true;
}

}


</script>

</head>

<body>
<form action="" name="FORM1" id="FORM1" onSubmit="javascript:validchk();">
<input type='radio' name='strMaxLen1' value='40' onClick="return validchk();">Normal Text (40 Char Max)&nbsp;&nbsp;
<input type='radio' name='strMaxLen1' value='20' onClick="return validchk();">Double Wide (20 Char Max)<br>
<input type='text' name='strText1' id='strText1' size='40' maxlength="40" onKeyUp="return validchk();">
<br>

<input type='radio' name='strMaxLen2' value='40' onClick="return validchk();">Normal Text (40 Char Max)&nbsp;&nbsp;
<input type='radio' name='strMaxLen2' value='20' onClick="return validchk();">Double Wide (20 Char Max)<br>
<input type='text' name='strText2' id='strText2' size='40' maxlength="40" onKeyUp="return validchk();">
<br>

<input type='radio' name='strMaxLen3' value='40' onClick="return validchk();">Normal Text (40 Char Max)&nbsp;&nbsp;
<input type='radio' name='strMaxLen3' value='20' onClick="return validchk();">Double Wide (20 Char Max)<br>
<input type='text' name='strText3' id='strText3' size='40' maxlength="40" onKeyUp="return validchk();">
<br>


<textarea rows="4" cols="40" name="strSample">

</textarea>

<input type="submit" name="Submit">

</form>
</body>
</html>
 
Two things stand out in your code. One is that you use "return validchk();" in your checkbox event handlers, but don't do anything with the return value.

Second,

onSubmit="javascript:validchk();"

shouldn't have the "javascript:" reference in it. Event handlers automatically look for the script function. And this IS the place where you would use "return validchk();", which would prevent the form from being submitted if there's an error. But you're not returning a value from the function anyway, so it really doesn't make sense in the onsubmit event.

Lee
 
the reason your code only picks up the value from the last text box is...
this code appears in each of the if statements
var chgLength = new String(document.FORM1.strText3.value);
document.FORM1.strSample.value=chgLength + "\r\n";
you re-write the value of the preview text box each time, and haven't made allowances for what was already there.

I would suggest changing your declaration "var chgLength" to "var chgLength#" where the # is the same as the text box if was created in.

then at the end do this...
document.FORM1.strSample.value=chgLength1 + "\r\n" + chgLength2 + "\r\n" + chgLength3 + "\r\n"

I don't know if it's possible to dump the values from the text boxes into an array, and then when you write the preview text box, it could be done in a loop. This would make for more reusable code.

Kevin Petursson
 
Thanks to both Trollacious and KPetursson for your inputs. Both are really appreciated. What is sometimes patently obvious to others is sometimes a wall for some of us thickheaded wannabe programmers.

Thank you for your quick responses and I am off to try to implement both of your solutions.

Have a great day.
Dougc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top