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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onLoad not working?

Status
Not open for further replies.

planeboy747

Technical User
Joined
Jan 11, 2005
Messages
41
Location
US
My onLoad function is not working in my form (onLoad="getRandom()") I'm using it to call a function to create a random number and then populate it into a text field. Perhaps I'm doing something wrong. To view the form go to:



Here is the html and script if you just want to look at that instead:
----------------------------------------------------------<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Order Request Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<Script Language=JavaScript>

////////////////////////////////
// remove terms and add form //
//////////////////////////////
function authorize()
{
var elem = document.getElementById("second");
elem.style.display = "block";
document.getElementById('accTerms').style.display = 'none';
}
//////////////////////////////////////////////
// create last/first name drop down fields //
////////////////////////////////////////////
function insertRows(isTable){

index = isTable.rows.length;
nextRow = isTable.insertRow(index);
isText1 = nextRow.insertCell(0);
isText2 = nextRow.insertCell(1);
index++;
index = index.toString();
nameStr1 = "Lname"+index;
nameStr2 = "Fname"+index;
txtStr1 = "Last Name "+index+":<br><input name="+nameStr1+" type='text' size='18' maxlength='18'>";
txtStr2 = "First Name "+index+":<br><input name="+nameStr2+" type='text' size='18' maxlength='22'>";
isText1.innerHTML = txtStr1;
isText2.innerHTML = txtStr2;
isText1.width ='25%'
isText2.width = '75%'
}

function adjustRows(isVal,isTable){

currRows = isTable.rows.length;
newRows = isVal;
if (currRows > 0){for (i=0; i<currRows-0; i++){isTable.deleteRow()}}
for (i=0; i<newRows; i++){insertRows(isTable)}
}
/////////////////////////////
// open approval section //
//////////////////////////
function openApprov()
{
var elem = document.getElementById("approvSection");
elem.style.display = "block";
document.getElementById('continue').style.display = 'none';
}
////////////////////////////
// create approval code //
//////////////////////////
var valid = new Array("2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");

function getRandom()
{
var num = Math.floor(Math.random() * valid.length);
return valid[num];
}

function randomString()
{
return "4" + getRandom() + getRandom() + getRandom();
}
</Script>
</head>
<body>
<p>&nbsp;</p>
<div id="accTerms">
<table width="550" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><strong>Acceptance of Terms</strong><br>
These sentences will highlight the acceptance of terms. These sentences will highlight
the acceptance of terms. These sentences will highlight the acceptance of terms.
These sentences will highlight the acceptance of terms.<br>
<br>
These sentences will highlight the acceptance of terms.These sentences will highlight
the acceptance of terms. These sentences will highlight the acceptance of terms.
These sentences will highlight the acceptance of terms. <br>
<br>
These sentences will highlight the acceptance of terms. These sentences will highlight
the acceptance of terms.</td>
</tr>
<tr>
<td align="right"><a href="#start" onClick="authorize(second)">Continue</a></td>
</tr>
</table>
</div>
<br>
<div name="second" id="second" style="display: none;">
<a name="start"></a>Please complete the following form to process your request.<br>
<form name="request">
<strong>*Number of customers: </strong><br>
<select name="select" onchange="adjustRows(this.value,nameSet)">
<option value="1" selected>Select one
<option value="1">1
<option value="2">2
<option value="3">3
<option value="4">4
<option value="5">5
<option value="6">6
<option value="7">7
<option value="8">8
<option value="9">9
</select>
<br>
<br>
<strong>Customer Name(s):</strong><br>
<table width="550" border="0" cellpadding="2" cellspacing="0" id="nameSet" class="cpySmall">
<tr>
<td width="25%" height="18" valign="top" class="cpySmall">*Last Name 1:<br>
<input name="textfield" type="text" size="18">
</td>
<td width="75%" height="18" valign="top" class="cpySmall">First Name 1: <br>
<input name="textfield" type="text" size="18">
</td>
</tr>
</table>
<div id="continue">
<table width="550" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><div align="right"><a href="#" onClick="openApprov(approvSection)">Continue</a></div>
</td>
</tr>
</table>
</div>
<br>
<div id="approvSection" style="display: none;">
<table width="550" border="0" cellspacing="0" cellpadding="0" id="acTable">
<tr>
<td valign="top"><strong>Authorization Code </strong></td>
</tr>
<tr>
<td valign="top">Your autorizaton code for this transaction is
<input name="appCode" type="text" size="4" style="color: #000080; text-transform: uppercase; font-weight: bold; border: 1px solid #FFFFFF" id="approvCode" onLoad="getRandom()">

. Complete your customer purchase orders and add those order numbers to the fields
below and submit. </td>
</tr>
</table>
<br>

<table width="550" border="0" cellspacing="0" cellpadding="0" id="onTable">
<tr>
<td colspan="2"><strong>Order Numbers </strong></td>
</tr>
<tr>
<td width="184">*Order Number 1<br>
<input name="on1" type="text" id="on1" size="18">
</td>
<td width="366">*Order Number 2<br>
<input name="on2" type="text" id="on2" size="18">
</td>
</tr>
<tr>
<td width="184">*Order Number 3<br>
<input name="on3" type="text" id="on3" size="18">
</td>
<td>*Order Number 4<br>
<input name="on4" type="text" id="on4" size="18">
</td>
</tr>
<tr>
<td>*Order Number 5<br>
<input name="on5" type="text" id="on5" size="18">
</td>
<td>*Order Number 6<br>
<input name="on6" type="text" id="on6" size="18">
</td>
</tr>
<tr>
<td>*Order Number 7<br>
<input name="on7" type="text" id="on7" size="18">
</td>
<td>*Order Number 8<br>
<input name="on8" type="text" id="on8" size="18">
</td>
</tr>
<tr>
<td>*Order Number 9<br>
<input name="on9" type="text" id="on9" size="18">
</td>
<td>&nbsp;</td>
</tr>
</table>
</form>
</div>
<br>
</body>
</html>
 
1. You do not need to call [tt]authorize(second);[/tt] [tt]authorize()[/tt] will work.

2. I do not see where you are calling the function?

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
hi chessbot, i'm calling it about halfway down through the code in this text field:

<input name="appCode" type="text" size="4" style="color: #000080; text-transform: uppercase; font-weight: bold; border: 1px solid #FFFFFF" id="approvCode" onLoad="getRandom()">
 
Ah, I see ...
You can only call onload from the body tag. Do something like
Code:
<body onload="document.forms['request'].elements['appCode'].value = randomString();">

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
it worked! cool!

If I disable this field, so that it stores that code without it being able to be changed, will it submit to the server? I've heard that disabled fields won't submit data...
 
Shouldn't be a problem...
Or you can just add [tt]onfocus="this.blur();"[/tt] to keep people from selecting it without disabling it.

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
You could just write it on the screen directly with document.write() and put the value in a hidden input then to use when the page is submitted, too.

Lee
 
chessbot, by the way, you are awesome! :)
 
I do like the document.write() idea.

What do I need to change?
 
lol thx--

Something like this?
Code:
Here is your unique product code: <b><script type="text/javascript"> document.writeln(randomString()); </script>. Please remember it ... etc. etc. etc.

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
cool. I was reading up on script tags earlier today and was thinking that's what i needed, but wasn't sure. So what recommended reading do you have for newbies in JS like me chessbot?
 
My favorite online tutorial?
--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
I'd add the hidden input there, too:

Here is your unique product code: <b><script type="text/javascript">
var rstring = randomString());
document.write(rstring);
document.write('<input type="hidden" name="randomstring" value="' + rstring + '">')
</script>.
 
hey one more question, how do i make sure the results are all caps on that script?
 
Just change the allowed array:
Code:
var valid = new Array('2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', ...);

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
oh yea, why didn't I think of that, thanks! :)
 
You're welcome...

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top