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

Would I use a Loop or Array to execute a form based on user inputs

Status
Not open for further replies.

oltran

Technical User
Dec 26, 2002
19
US
Great on the subject line huh?

Ok, I have a form that will add extra fields based on how many times a user clicks on a link. Then when the user clicks on the execute button it should pass the information entered to notepad. I would like this to work for as many extra fields as the user adds. But it will only work for one extra field at this point because I only have variables & if statements setup for one. I would like to have a loop or something that will accomodate as many fields as the user adds. Any help or a point in the right direction would be appreciated!

The full code is at the bottom, but here are the lines of code I need to loop? or something:

HourlyOrFlatT1 = ""
TaskAmountT1 = ""

if(document.Form_1.HourlyOrFlatT1_field.value != ""){HourlyOrFlatT1 = document.Form_1.HourlyOrFlatT1_field.value}
if(document.Form_1.TaskAmountT1_field.value != ""){TaskAmountT1 = document.Form_1.TaskAmountT1_field.value}

if(HourlyOrFlatT1 != ""){Form_1_content += HourlyOrFlatT1 + "~"}
if(TaskAmountT1 != ""){Form_1_content += TaskAmountT1 + "~"}

Here is a sample of my code so far:

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot; &quot;<html lang=&quot;en&quot;>
<head>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;><!-- Begin

function generate(){

//Define variables that will be assigned to the values entered by the user
Client = &quot;&quot;
AcctMgr = &quot;&quot;
HourlyOrFlatT1 = &quot;&quot;
TaskAmountT1 = &quot;&quot;
NewSession = &quot;&quot;

//Define the variable that will be composed of the above variables.
Form_1_content = &quot;&quot;


//Determine if a value is entered in one of the form fields, if so, assign it to its variable.
if(document.Form_1.Client_field.value != &quot;&quot;){Client = document.Form_1.Client_field.value}
if(document.Form_1.AcctMgr_field.value != &quot;&quot;){AcctMgr = document.Form_1.AcctMgr_field.value}
if(document.Form_1.HourlyOrFlatT1_field.value != &quot;&quot;){HourlyOrFlatT1 = document.Form_1.HourlyOrFlatT1_field.value}
if(document.Form_1.TaskAmountT1_field.value != &quot;&quot;){TaskAmountT1 = document.Form_1.TaskAmountT1_field.value}


//Compile the 'Form_1_content' variable based on the variables that were assigned a value
//This block of code uses the ~ character instead of the \n to make a new line for vbscript.
if(Client != &quot;&quot;){Form_1_content += Client + &quot;~A~&quot;}
if(AcctMgr != &quot;&quot;){Form_1_content += AcctMgr + &quot;~&quot;}
if(HourlyOrFlatT1 != &quot;&quot;){Form_1_content += HourlyOrFlatT1 + &quot;~&quot;}
if(TaskAmountT1 != &quot;&quot;){Form_1_content += TaskAmountT1 + &quot;~&quot;}

//Define starting & ending variables
Form_1_start = &quot;&quot;
Form_1_end = &quot;End Stuff Here&quot;

//Tell function where to dump the value generated.
document.Form_1.output.value = Form_1_start + Form_1_content + Form_1_end

}


//This will add extra 'task' forms
addlink = 2;
tNum = 0;
function displayTask()
{
tNum = tNum + 1;
var str='<table bgcolor=&quot;#DDDDDD&quot; align=&quot;center&quot; border=&quot;1&quot; width=&quot;100%&quot;>';
str+= ' <tr>';
str+= ' <td bgcolor=&quot;#000000&quot; colspan=&quot;2&quot;><font color=&quot;#FFFFFF&quot;><a name=&quot;Task' + tNum + '&quot;>Task ' + tNum + '</a></font></td>';
str+= ' </tr>';
str+= ' <tr>';
str+= ' <td colspan=&quot;2&quot;>Hourly/Flat: <select name=&quot;HourlyOrFlatT' + tNum + '_field&quot; size=&quot;1&quot; class=&quot;txt&quot;>';
str+= ' <option value=&quot;1&quot; selected>Hourly';
str+= ' <option value=&quot;2&quot;>Flat Rate';
str+= ' <option value=&quot;3&quot;>Per Piece</option>';
str+= ' </select></td>';
str+= ' </tr>';
str+= ' <tr>';
str+= ' <td colspan=&quot;2&quot;>Amount: <input type=&quot;TEXT&quot; size=&quot;10&quot; name=&quot;TaskAmountT' + tNum + '_field&quot; value class=&quot;txt&quot;></td>';
str+= ' </tr>';
str+= ' <TR> ';
str+= ' <TD><a href=&quot;#&quot; onClick=&quot;displayTask();return false;&quot;>Add Task</a></span></TD>';
str+= ' </TR>';
str+= ' </table>';
str+= '<div id=&quot;somediv' + addlink + '&quot;></div>';
somediv = 'somediv' + tNum;
document.all[somediv].innerHTML = str;
addlink = addlink + 1;
}

//--></SCRIPT>

<SCRIPT LANGUAGE=vbscript>
function ShowPassed
Dim strPassedValue
Dim NewSession
Dim strUserName
Dim strPassword
Dim ss
Dim starttime,waittime,currenttime
waittime = 3
strPassedValue = document.Form_1.output.value
NewSession = document.Form_1.NewSession_field.value
strUserName = document.Form_1.UserName_field.value + &quot;{enter}&quot;
strPassword = document.Form_1.Password_field.value + &quot;{enter}&quot;
if NewSession = 1 then
MsgBox(&quot;Clicking OK will begin process.&quot;)
Set ss = CreateObject(&quot;WScript.Shell&quot;)
ss.run &quot;C:\WINNT\NOTEPAD.EXE&quot;,1
starttime = Timer()
currenttime = starttime
Do Until currenttime-starttime > waittime
'Do nothing
currenttime = Timer()
Loop
ss.SendKeys strUserName
starttime = Timer()
currenttime = starttime
Do Until currenttime-starttime > waittime
'Do nothing
currenttime = Timer()
Loop
ss.SendKeys strPassword
starttime = Timer()
currenttime = starttime
Do Until currenttime-starttime > waittime
'Do nothing
currenttime = Timer()
Loop
ss.SendKeys &quot;1{enter}JOBREQUEST{ENTER}3&quot; + strPassedValue
Set ss = nothing
else
MsgBox(&quot;ActivateApp, still working on...&quot;)
end if
end function

</SCRIPT>

<meta name=&quot;GENERATOR&quot; content=&quot;Microsoft FrontPage 4.0&quot;>
<meta name=&quot;ProgId&quot; content=&quot;FrontPage.Editor.Document&quot;>
</head>

<body class=&quot;txt&quot;>
<form name=&quot;Form_1&quot;>
<table bgcolor=&quot;#DDDDDD&quot; align=&quot;center&quot; border=&quot;1&quot; width=&quot;100%&quot;>
<tr>
<td bgcolor=&quot;#000000&quot; colspan=&quot;2&quot;><font color=&quot;#FFFFFF&quot;>Create Job Request</font></td>
</tr>
<tr>
<td colspan=&quot;2&quot;>Client number: <input type=&quot;TEXT&quot; size=&quot;10&quot; name=&quot;Client_field&quot; value class=&quot;txt&quot;></td>
</tr>
<tr>
<td>Account Manager: <select name=&quot;AcctMgr_field&quot; size=&quot;1&quot; class=&quot;txt&quot;>
<option value=&quot;0&quot; selected>Select
<option value=&quot;1&quot;>John Doe
<option value=&quot;2&quot;>Jane Jones</option>
</select></td>
<tr>
<TD><a href=&quot;#&quot; onClick=&quot;displayTask();return false;&quot;>Add Task</a></TD>
</tr>
</table>
<div id=&quot;somediv1&quot;></div>
<table bgcolor=&quot;#DDDDDD&quot; align=&quot;center&quot; border=&quot;1&quot; width=&quot;100%&quot;>
<tr>
<td>Run in new Session? <select name=&quot;NewSession_field&quot; size=&quot;1&quot; class=&quot;txt&quot;>
<option value=&quot;1&quot; selected>Yes
<option value=&quot;2&quot;>No</option>
</select></td>
<td>user name: <input type=&quot;TEXT&quot; size=&quot;10&quot; name=&quot;UserName_field&quot; value class=&quot;txt&quot;>
password: <input type=&quot;password&quot; size=&quot;10&quot; name=&quot;Password_field&quot; value class=&quot;txt&quot;></td>
</tr>
<tr>
<td align=&quot;center&quot;><input type=&quot;RESET&quot; value=&quot;Default Settings&quot;> <input type=&quot;SUBMIT&quot; value=&quot;Save Settings&quot;></td>
<td align=&quot;center&quot;><input name=&quot;button&quot; type=&quot;button&quot; onclick=&quot;generate()&quot; value=&quot;Append&quot;> <input type=&quot;button&quot; value=&quot;Execute!&quot; id=&quot;button2&quot; name=&quot;button2&quot; onclick=&quot;generate(),ShowPassed()&quot;></td>
</tr>
<tr>
<td colspan=&quot;2&quot;>Form 1 Output: <textarea type=&quot;TEXT&quot; name=&quot;output&quot; rows=&quot;10&quot; cols=&quot;55&quot;></textarea></td>
</tr>
</table>

</form>
</body>
</html>
 
well, this is a possible direction:

- create a variable in your head script, set to 1
- each time the user clicks your add table button, include a call to a function that increments that variable
- when each table is created, name/id it dynamically also; use that variable
- upon 'execute', read the new value of the variable
- create a for loop that utitlizes your first code snippet
- use eval() to dynamically create the correct name/id of each table using the variable again or access using forms elements (complex)

what do you think?

- g


 
Great! I figured out how to use the eval() with my variables. Man, this opens up a bunch of ideas.

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top