Connecting Tech Pros Worldwide Help | Site Map

How to add extra input box in the same form when the button is clicked??

Newbie
 
Join Date: Jul 2008
Posts: 30
#1: Aug 10 '08
I hava a form, which has five lines to enter at start. I want to have a button to add more lines when it is clicked. However, I am having trouble with it. This is written in vbscipt and ADO.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <%
  3. Const linesNo = 5
  4. Dim counter
  5.  
  6. <html>
  7. .....
  8.  
  9. <form action='return_form.asp' method=post>
  10. <input type=hidden name='task' value='add'>
  11. <table>
  12. .....
  13.  
  14.  
  15.  
  16. <%
  17. if request("task")="lines" then
  18.      linesNo = linesNo + 5
  19.      response.write "return_forms.asp"     
  20. end if 
  21.  
  22. counter = 0
  23. Do While counter < linesNo
  24. %>
  25.  
  26. <tr>
  27. <td  width=20%><input type=text size="20" maxlength="30" name='part_no_<%=counter%>' value='<%=request("part_no_")%>'/></td>
  28. </tr>
  29.  
  30. <%
  31. counter = counter + 1
  32. Loop    
  33. %>
  34.  
  35. <tr>
  36. <td width=20%><form action='return_form.asp' method=post><input type=hidden name='task' value='lines'>
  37. <input type=submit name='lines' value='Add Lines'></td>
  38. </tr>
  39.  
  40. ......................(some codes)
  41. <tr>
  42. <td></td>&nbsp;<td><input type=submit value="Save & Print"></td>
  43. </tr>
  44.  
  45.  
  46. </table>
  47.  
  48. </form>
  49.  
Thanks
DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 930
#2: Aug 11 '08

re: How to add extra input box in the same form when the button is clicked??


Hi yimma216,

What actually happens when you run this? Does it error?

I think the problem is probably that you have two form opening tags and only one form closing tag. Make sure that both of your forms are closed correctly if you did intend to have two of them on your page.

Also you have two hidden variables called 'task' both with different values which I think is probably passing the wrong value to Request("task"). You can debug this by checking the value with a Response.Write Request("task") just before you test whether it's value is equal to "lines".

Take a look at these points and let me know how you get on.

Dr B
Newbie
 
Join Date: Jul 2008
Posts: 30
#3: Aug 11 '08

re: How to add extra input box in the same form when the button is clicked??


yes, there is a problem in the code. I found out i could not use nested form in html. Is moving on now.
Newbie
 
Join Date: Jul 2008
Posts: 30
#4: Aug 12 '08

re: How to add extra input box in the same form when the button is clicked??


hi Dr,

I have fixed the problem you mentioned and got some other advices.
However, when I tried to run the code, the application timed out.

Expand|Select|Wrap|Line Numbers
  1. <%
  2. Dim linesNo 
  3. Dim counter
  4.  
  5. 'default value for the input
  6. linesNo = 5
  7.  
  8. Set conn = Server.CreateObject("ADODB.Connection")
  9. Set cmd = Server.CreateObject("ADODB.Command")
  10. Set cmd2 = Server.CreateObject("ADODB.Command")
  11. ..................etc 
  12.  
  13. 'form submit button was clicked
  14. If request("save")<>"" Then
  15. conn.Open  "Driver={SQL Native Client};Server=cslproxy;database=gra;Trusted_Connection=yes;"
  16.  
  17. counter = 0
  18.  
  19.       Set cmd.ActiveConnection = conn
  20.       cmd.CommandText = "add_goods"
  21.       cmd.CommandType = 4
  22.       cmd.Parameters.Refresh
  23.       ...............etc
  24.       set rs = cmd.execute
  25.  
  26. Do While counter < request("lines") 
  27.     If request("part_no_"&counter) <> "" then
  28.       Set cmd2.ActiveConnection = conn
  29.       cmd2.CommandText = "add_parts"
  30.       cmd2.CommandType = 4
  31.       cmd2.Parameters.Refresh
  32.       cmd2.Parameters("@gra_id") =  rs(0)
  33.     .......................etc
  34.        cmd2.execute
  35.     End If 
  36.     counter = counter + 1
  37. Loop
  38.  
  39.  
  40. response.redirect "print_out.asp?gra_id=" & rs(0)
  41. rs.close
  42. conn.close
  43. else
  44.  
  45. ' form submit wasn't clicked...
  46.         ' we had a hidden lines value
  47.         if request("lines")<>"" then
  48.             ' populate linesNo with that value
  49.             linesNo = request("lines")
  50.             ' Add Lines submit button was clicked
  51.             if request("addLines")<>"" then
  52.                 ' so add five to current lines value
  53.                 linesNo = linesNo + 5
  54.                 'response.write vbcrlf & linesNo
  55.             end if
  56.         end if
  57.  
  58.  
  59.  
  60. %>
  61. <html>
  62. <head></head>
  63. <body>
  64. ...............................etc
  65.  
  66. <form action='return_form.asp' method=post>
  67. <input type=hidden name='lines' value='<%=linesNo%>'>
  68. ........etc
  69.  
  70.  
  71. <table>
  72.   <tr>
  73.     <td width=20%>Part No</td><td  width=40% colspan=2>Description</td></td><td width=20%>Quantity</td>
  74.   </tr>
  75.  
  76. <%    
  77. counter = 0
  78. Do While counter < linesNo
  79. %>
  80.  
  81.   <tr>
  82.       <td  width=20%><input type=text size="20" maxlength="30" name='part_no_<%=counter%>' value='<%=request("part_no_"&counter)%>'/></td><td  width=40% colspan=2><input type=text size='70' maxlength='100' name='desc_<%=counter%>' value='<%=request("desc_"&counter)%>' /></td><td><input type=text size="5" maxlength="10" name='quantity_<%=counter%>' value='<%=request("quantity_"&counter)%>' /></td>        
  83.   </tr>
  84.  
  85.  <%
  86.  counter = counter + 1
  87.   Loop
  88.  %>
  89.  
  90. <tr>
  91.   <td width=20%><input type=submit name='addLines' value='Add Lines'></td>
  92. </tr>
  93. </table>
  94. ........................etc
  95.  
  96.  
  97.   <tr>
  98.      <td></td>&nbsp;<td><input type=submit name="save" value="Save & Print"></td>
  99.   </tr>
  100.  
  101. </table>
  102.  
  103. </form>
  104.  
  105. <%
  106. conn.close
  107. End if
  108.  
  109. %>
  110. </body>
  111. </html>
  112.  
Newbie
 
Join Date: Jul 2008
Posts: 30
#5: Aug 12 '08

re: How to add extra input box in the same form when the button is clicked??


Quote:

Originally Posted by yimma216

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. Do While counter < request("lines") 
  4.     If request("part_no_"&counter) <> "" then
  5.       Set cmd2.ActiveConnection = conn
  6.       cmd2.CommandText = "add_parts"
  7.       cmd2.CommandType = 4
  8.       cmd2.Parameters.Refresh
  9.       cmd2.Parameters("@gra_id") =  rs(0)
  10.     .......................etc
  11.        cmd2.execute
  12.     End If 
  13.     counter = counter + 1
  14. Loop
  15.  
  16.  

I am suspecting the counter < request("lines").
But do not know how to refine the code
jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Posts: 2,690
#6: Aug 12 '08

re: How to add extra input box in the same form when the button is clicked??


When I get scripts timing out, I go back and check through my loops. change them one at a time to one shot statements (in other words, make them not loop) and that will usually tell me which loop is at fault. That said, as long as the loop advances (and you have a counter = counter + 1 line) the only time I have this problem is if I have an error on my page and a "on error ..." error handler which keeps the page moving despite the error. Try commenting out your error handler to see what is really going on.

Jared
Newbie
 
Join Date: Jul 2008
Posts: 30
#7: Aug 12 '08

re: How to add extra input box in the same form when the button is clicked??


Quote:

Originally Posted by jhardman

When I get scripts timing out, I go back and check through my loops. change them one at a time to one shot statements (in other words, make them not loop) and that will usually tell me which loop is at fault. That said, as long as the loop advances (and you have a counter = counter + 1 line) the only time I have this problem is if I have an error on my page and a "on error ..." error handler which keeps the page moving despite the error. Try commenting out your error handler to see what is really going on.

Jared


Thank you Jared.
The mistakes were the request("lines") - (someone told me). I tried casting that to integer and the system seems to be happy now.
Reply


Similar ASP / Active Server Pages bytes