Language: asp (GeSHi-highlighted)<!--METADATA TYPE="typelib" uuid="00000205-0000-0010-8000-00AA006D2EA4" --><%Option ExplicitSession.CodePage=65001Response.Charset="UTF-8" ' no browser caching of this page !! to be used on all pagesResponse.Expires=-1Response.ExpiresAbsolute = Now() - 1 ' do not allow proxy servers to cache this page !! to be used on all pagesResponse.CacheControl="private"Response.CacheControl="no-cache"Response.CacheControl="no-store" Dim cmd, conn, RS, cmdText, param, numAffected Set cmd = Server.CreateObject("ADODB.Command")Set conn = Server.CreateObject("ADODB.Connection")conn.Open Application("examples") cmdText = "SELECT code, description FROM states"cmd.commandText = cmdTextSet cmd.ActiveConnection = conn Set rs = cmd.Execute(numAffected,,adCmdText)if NOT (rs.bof AND rs.eof) then Dim tmpStr tmpStr = "<scr" & "ipt type=" & chr(34) & "text/javascr" & "ipt" & chr(34) & ">" & vbLF ' declare a client side array tmpStr = tmpStr & " var csArray=new Array();" & vbLF & vbLF Dim idx, item idx = 0 ' JavaSccript arrays are zero based. ' fill the array from our recordset While NOT rs.eof tmpStr = tmpStr & " csArray["& idx &"]=(['" for each item in rs.fields tmpStr = tmpStr & item & "','" next ' backoff the trailing ,' and terminate the line with ]); tmpStr = Left(tmpStr, Len(tmpStr)-2) & "]);" & vbLF ' increment our array index idx = idx + 1 rs.movenext Wend tmpStr = tmpStr & "</scr" & "ipt>" & vbLFend ifrs.closeSet rs = nothingSet cmd=nothingconn.closeSet conn=nothing Dim posted, numRows posted = ""' if we have a post - process itif request.servervariables("HTTP_METHOD")="POST" then if CInt(request.form("numRows")) >= 0 then ' how many rows were posted? numRows = CInt(request.form("numRows")) ' loop through the rows and associated the checkboxes ' to the selected values in the select lists for idx=0 to numRows if request.form("cb"&idx) = "" then posted = posted & "Row " & idx & " Not Checked " else posted = posted & "Row " & idx & " Checked " end if if request.form("sel"&idx) = "" then posted = posted & " Value=Empty<br>" else posted = posted & " Value=" & request.form("sel"&idx) & "<br>" end if next end if end if%>
Language: javascript (GeSHi-highlighted)<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="author" content="Roderick Divilbiss"><meta name="copyright" content="© 2005, 2006 Roderick Divilbiss"><title>Adding Dynamic Rows To Form</title><!--write our database results as an array--><% response.write tmpStr %><script type="text/javascript"><!--var numberOfRows = 0; function insertRow(tblId) { var tbl = document.getElementById(tblId); var iteration = tbl.tBodies[0].rows.length; newRow = tbl.tBodies[0].insertRow(-1); var newCell = newRow.insertCell(0); var newCell1 = newRow.insertCell(1); var el = document.createElement('input'); el.type = 'checkbox'; el.name = 'cb' + iteration; el.id = 'cb' + iteration; newCell1.appendChild(el); var newCell2 = newRow.insertCell(1); var newCell1 = newRow.insertCell(1); var sel = document.createElement('select'); sel.name = 'sel' + iteration; sel.id = 'sel' + iteration; // build the select from the retrieved database rows // we stored in a JS array. for (var idx=0;idx<csArray.length;idx++) { sel.options[idx] = new Option(csArray[idx][0], csArray[idx][1]); } newCell2.appendChild(sel); // update hidden rows count field document.getElementById('numRows').value=iteration;} function deleteAllRows(tblId) { var tbl = document.getElementById(tblId); for (var i=tbl.tBodies[0].rows.length-1; i>=0; i--) { tbl.tBodies[0].deleteRow(i); }}//--></script></head> <body><!--posted results if any--><%=posted%> <form action="dynamicRows.asp" method="post"><p><input value="Add" onclick="insertRow('tblInsertRow');" type="button"><input value="Reset" onclick="deleteAllRows('tblInsertRow');" type="button"><input value="Submit" type="submit"></p><table id="tblInsertRow" border="0" cellspacing="0"> <thead> <tr> <th colspan="2">Header</th> </tr> </thead> <tbody></tbody></table><input id="numRows" name="numRows" type="hidden" value="0"></form></body></html>
Language: asp (GeSHi-highlighted)<!--METADATA TYPE="typelib" uuid="00000205-0000-0010-8000-00AA006D2EA4" --><%Option ExplicitSession.CodePage=65001Response.Charset="UTF-8" ' no browser caching of this page !! to be used on all pagesResponse.Expires=-1Response.ExpiresAbsolute = Now() - 1 ' do not allow proxy servers to cache this page !! to be used on all pagesResponse.CacheControl="private"Response.CacheControl="no-cache"Response.CacheControl="no-store" Dim cmd, conn, RS, cmdText, param, numAffected Set cmd = Server.CreateObject("ADODB.Command")Set conn = Server.CreateObject("ADODB.Connection")conn.Open Application("examples") cmdText = "SELECT code, description FROM states"cmd.commandText = cmdTextSet cmd.ActiveConnection = conn Set rs = cmd.Execute(numAffected,,adCmdText) function getSelect(id, selected) Dim tmpStr tmpStr = "<select id="& chr(34) & id & chr(34) &" name="& chr(34) & id & chr(34) &">" Dim item ' fill the select from our recordset rs.movefirst While NOT rs.eof if (rs("description")=selected) then tmpStr = tmpStr & "<option selected value="&chr(34)&rs("description")&chr(34)&">"&rs("code")&"</option>" else tmpStr = tmpStr & "<option value="&chr(34)&rs("description")&chr(34)&">"&rs("code")&"</option>" end if rs.movenext Wend tmpStr = tmpStr & "</select>" getSelect=tmpStrend function function getCheckBox(id, value) Dim tmpStr if value="" then tmpStr = "<input id="& chr(34) & id & chr(34) &" name="& chr(34) & id & chr(34) &" type=""checkbox"">" else tmpStr = "<input id="& chr(34) & id & chr(34) &" name="& chr(34) & id & chr(34) &" type=""checkbox"" checked>" end if getCheckBox=tmpStrend function Dim theArray theArray = "<scr" & "ipt type=" & chr(34) & "text/javascr" & "ipt" & chr(34) & ">" & vbLF ' declare a client side array theArray = theArray & " var csArray=new Array();" & vbLF & vbLF Dim idx, item idx = 0 ' JavaSccript arrays are zero based. ' fill the array from our recordset While NOT rs.eof theArray = theArray & " csArray["& idx &"]=(['" for each item in rs.fields theArray = theArray & item & "','" next ' backoff the trailing ,' and terminate the line with ]); theArray = Left(theArray, Len(theArray)-2) & "]);" & vbLF ' increment our array index idx = idx + 1 rs.movenext Wend theArray = theArray & "</scr" & "ipt>" & vbLF Dim posted, numRows, cb(), sel() posted = ""numRows = 0 if request.servervariables("HTTP_METHOD")="POST" then if CInt(request.form("numRows")) >= 0 then numRows = CInt(request.form("numRows")) redim cb(numRows) redim sel(numRows) posted = "<tbody>" for idx=0 to numRows posted = posted & "<tr><td></td><td></td><td>" if request.form("sel"&idx) = "" then sel(idx)="" posted = posted & getSelect("sel"&idx, "") & "</td>" else sel(idx)=request.form("sel"&idx) posted = posted & getSelect("sel"&idx, request.form("sel"&idx)) & "</td>" end if if request.form("cb"&idx) = "" then cb(idx)=false posted = posted & "<td>" & getCheckBox("cb"&idx, "") & "</td>" else cb(idx)=true posted = posted & "<td>" & getCheckBox("cb"&idx, "checked") & "</td>" end if posted = posted & "</tr>" next posted = posted & "</tbody>" end ifend if if posted="" then posted="<tbody></tbody>"end if rs.closeSet rs = nothingSet cmd=nothingconn.closeSet conn=nothing%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="en"> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="author" content="Roderick Divilbiss"><meta name="copyright" content="© 2005, 2006 Roderick Divilbiss"><title>Whatever</title><%=theArray%><script type="text/javascript"><!--var numberOfRows = <%=numRows%>; function insertRow(tblId) { var tbl = document.getElementById(tblId); var iteration = tbl.tBodies[0].rows.length; newRow = tbl.tBodies[0].insertRow(-1); var newCell = newRow.insertCell(0); //newCell.innerHTML = iteration; var newCell1 = newRow.insertCell(1); var el = document.createElement('input'); el.type = 'checkbox'; el.name = 'cb' + iteration; el.id = 'cb' + iteration; newCell1.appendChild(el); var newCell2 = newRow.insertCell(1); //newCell.innerHTML = 'right ' + iteration; var newCell1 = newRow.insertCell(1); var sel = document.createElement('select'); sel.name = 'sel' + iteration; sel.id = 'sel' + iteration; for (var idx=0;idx<csArray.length;idx++) { sel.options[idx] = new Option(csArray[idx][0], csArray[idx][1]); } newCell2.appendChild(sel); document.getElementById('numRows').value=iteration; } function deleteAllRows(tblId) { var tbl = document.getElementById(tblId); for (var i=tbl.tBodies[0].rows.length-1; i>=0; i--) { tbl.tBodies[0].deleteRow(i); }}//--></script></head> <body><noscript><p>You need JavaScript to view this example.</p></noscript><h2>How can you dynamically add rows and fields to a table?</h2><p><%if numRows<>"<tbody></tbody>" then for idx=0 to numRows response.write "Row "&idx&": Selected="&sel(idx)&" Checked="&cb(idx)&"<br>" nextend if%></p><form action="dynamicRows2.asp" method="post"><p><input value="Add" onclick="insertRow('tblInsertRow');" type="button"><input value="Reset" onclick="deleteAllRows('tblInsertRow');" type="button"><input value="Submit" type="submit"></p><table id="tblInsertRow" border="0" cellspacing="0"> <thead> <tr> <th colspan="4">Header</th> </tr> </thead> <%=posted%></table><input id="numRows" name="numRows" type="hidden" value="<%=numRows%>"></form></body> </html>
Language: asp (GeSHi-highlighted)if CInt(request.form("numRows")) >= 0 then
Language: asp (GeSHi-highlighted)<%if posted<>"<tbody></tbody>" then for idx=0 to numRows response.write "Row "&idx&": Selected="&sel(idx)&" Checked="&cb(idx)&"<br>" nextend if%>
Language: asp (GeSHi-highlighted)<%=tmpStr%>