Forum Moderators: open

Message Too Old, No Replies

Function problem

Function

         

mica

4:25 am on Jan 12, 2008 (gmt 0)

10+ Year Member



Newbie here. Please help me with this code... it's not working...

function downline1(){
B = document.frmOne.txtSecondNumber.value
B = Number(B)
no=1;

document.write("<table>");
while (no<=B)
{
document.write("<tr>");
document.write("<td>Referral no."+no+" <input type='text' name='ref"+no+"'></td>");
document.write("<td>Personal Purchase Made."+no+" <input type='text' name='PP"+no+"'></td>");
document.write("</tr>");
no=no+1;
}
document.write("</table>");
document.write("<input type='button' value='next' onclick='lvlone();' > ");

}

<script language="javascript">

function lvlone() {
alert ("Hello");
}

</script>

I think there's a problem with this one...
document.write("<input type='button' value='next' onclick='lvlone();' > ");

Basically what i need to do is... i'll input a number in the first textbox and after i click next it'll display the number of textboxes that i input... and in those textboxes i'll input a number and after i click next display that the number of textboxes i input sort of like a tree until i get to the fifth level down.... but im stuck with the second step and i cant go forward to the adding part. Help Please!

daveVk

6:18 am on Jan 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi

B = Number(B)
needs to be
B = new Number(B)

Also good practice to use "var" and ";" as below

function downline1(){
var B = document.frmOne.txtSecondNumber.value;
B = new Number(B);
var no=1;

As a minimal level of debugging tool turn javascript error reporting on
to get error line number.

Welcome to forum.

Dabrowski

8:40 pm on Jan 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This function really isn't very robust, what if they don't type a number?

var B = document.frmOne.txtSecondNumber.value;  
var no=1;
if( isNaN( B)) return;

There is no need to use the Number function, simply return if what they typed is (N)ot (a) (N)umber.

Also, we don't use

no=no+1;
, we can use
no++;
instead.

You will probably find this page useful and explains about the shorthand operators you can use in JavaScript.
[w3schools.com...]

Welcome to the forum!

mica

1:24 am on Jan 14, 2008 (gmt 0)

10+ Year Member



Thanks you guys... im so new at this...

first level is okay... i mean when i input number on the first textbox displayed im able to go to the next level its just that when i get to that level i get an error and im not able to go to the third level anymore... so im stuck...

here's the complete code...

<FORM NAME = frmOne>
No. of Referrals:
<INPUT TYPE = Text NAME = txtSecondNumber SIZE = 5 value ="" >
<P>
<Input Type = Button NAME = b1 VALUE = "Submit" onClick = "downline1();">
</FORM>


<script language="javascript">

function downline1(){
var B = document.frmOne.txtSecondNumber.value;
B = new Number(B);
var no=1;

document.write('<form name="formTwo">');

document.write('<table>');
while (no<=B)
{
document.write('<tr>');
document.write('<td>Referral no.'+no+' <input type="text" name="ref'+no+'"></td>');
document.write('<td>Personal Purchase Made.'+no+' <input type="text" name="PP'+no+'"></td>');
document.write('</tr>');
no++;
}
document.write('</table>');
document.write('<input type="button" name="b2" value="next" onclick="lvlone();">');
document.write('</form>');
//document.formTwo.b2.onclick = lvlone();
}

</script>

<script language="text/javascript">

function lvlone() {
alert ("Hello");
}
</script>

for now i just use an alert for lvlone...

so... repeating what i posted...

I think there's a problem with this one...
document.write("<input type='button' value='next' onclick='lvlone();' > ");

Basically what i need to do is... i'll input a number in the first textbox and after i click next it'll display the number of textboxes that i input... and in those textboxes i'll input a number and after i click next display that the number of textboxes i input sort of like a tree until i get to the fifth level down.... but im stuck with the second step and i cant go forward to the adding part. Help Please!

daveVk

12:01 pm on Jan 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Before proceeding consider if the task would be better done on the server side, provide a reasonable number of empty slots initially and send to server to rebuild form when out of free slots.

I have had trouble in past with dynamic generated forms, the forms look Ok, but think data was lost if browse away from form, perhaps this has been fixed?

If you decide to continue, you will need to use innerHTML or Dom creation code instead of document.write as document.write only adds code to end of document.

Using innerHTML it becomes ( rough starting point )

<FORM NAME = frmOne>
No. of Referrals:
<INPUT TYPE = Text NAME = txtSecondNumber SIZE = 5 value ="" >
<P>
<Input Type = Button NAME = b1 VALUE = "Submit" onClick = "downline1();">
</FORM>
<form name="formTwo">
<div id="L1'></div>
<form>

<script language="javascript">

function downline1(){
var B = document.frmOne.txtSecondNumber.value;
B = new Number(B);
addLevel( 1, B );

}

function addLevel( level, max } {

var aS = '<table>';
var no=1;
while (no<=max) {
aS += '<tr>';
aS += '<td>Referral no.'+no+' <input type="text" name="ref'+no+'"></td>';
aS += '<td>Personal Purchase Made.'+no+' <input type="text" name="PP'+no+'"></td>';
aS += '</tr>';
no++;
}
aS += '</table>';
aS += '<div id=L' + (level+1) + '></div>'; // next level expands here?
aS += '<input type="button" name="b2" value="next" onclick="lvlone();">';
el=document.getElementById( "L" + level );
el.innerHTML = aS;
}

</script>

<script language="text/javascript">

function lvlone() {
alert ("Hello");
// get max and call addlevel for next level
}
</script>

addLevel would need beefing up to produce correct code for any level, indeed any branch of tree?

good luck

Dabrowski

3:52 am on Jan 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would suggest you have a look at JavaScript 'for' loops. Change this:

var no=1;  
while (no<=max) {
aS += '<tr>';
aS += '<td>Referral no.'+no+' <input type="text" name="ref'+no+'"></td>';
aS += '<td>Personal Purchase Made.'+no+' <input type="text" name="PP'+no+'"></td>';
aS += '</tr>';
no++;
}

To:

for( var no = 1; no <=max; no++) { 
aS += '<tr>';
aS += '<td>Referral no.'+no+' <input type="text" name="ref'+no+'"></td>';
aS += '<td>Personal Purchase Made.'+no+' <input type="text" name="PP'+no+'"></td>';
aS += '</tr>';
}

Shame on you DaveVK!

daveVk

4:19 am on Jan 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dabrowski I agree, left loop in original form out of lazyness and to show relationship to original( excuse ). No good reason for string concatonization either.

for( var no = 1; no <=max; no++) {
aS += '<tr>'
+ '<td>Referral no.'+no+' <input type="text" name="ref'+no+'"></td>'
+ '<td>Personal Purchase Made.'+no+' <input type="text" name="PP'+no+'"></td>'
+ '</tr>';
}

[edited by: daveVk at 4:20 am (utc) on Jan. 15, 2008]