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

SQL Insert from Dynamic Form 1

Status
Not open for further replies.

scripter50

IS-IT--Management
Dec 9, 2002
35
US
I have an ASP page with a dynamically generated form, where the user inputs a number, presses proceed and the form is generated. Then I need to insert the data into a table and that's where I'm lost. Here's a code snipet.. thanks in advance for your assistance!

<form action = "thispage.asp">

<input type=”text” name=”qty">
<input type="submit" value="Proceed">

<% for i = 1 to Request.Form(“qty”) %>

<table>
<tr>
<td>First Name:</td>
<td><input type=”text” name=”first_<%=i%>”></td>
etc. etc. etc……

<% next %>

<input type = “submit” name=”btnSubmit”>

<% if Request.Form(“btnSubmit”) <> “” then

for i = 1 to Request.Form(“addItem”)

sFirstName = Request.Form(“first” &i)
etc. etc.

sql = “insert into table (Firstname, etc.etc.) “ & _
“ values(“ & sFirstName & “,” etc. etc.)

objconn.execute sql

next

end if %>
 
Remember that the entire ASP code will run and be done running by the time the user fills out the form so that script won't have be able to read what the user entered until after the thing is submitted.
 
It looks like you want an ASP page that will actually run 3 times.

The first time it will show a form asking for the Quantity.
The second time it will use that quantity to build an input form.
The third time it will process the input from the 2nd form and insert it into the database.

Code:
<% 
if Request.Form("btnProcede") = "" then
 'first time script is run, show Quantity page
%>
<html>
 <head>
   <title>Sample for scripter50</title>
 <head>
 <body>
  <form action="thispage.asp" method="post">
    Enter Quantity: 
    <input type="text" name="qty">
    <br>
    <input type="submit" name="btnProcede" value="Proceed">
  </form>
 </body>
</html>
<%
  Response.End

elseif Request.Form("btnProcede") = "Proceed" then
 'second time script is run
  iQTY = Request.Form("qty")

  'Return to Quantity page if not positive number
  if Not IsNumeric(iQTY) then Response.Redirect "thispage.asp"
  if (CINT(iQTY) > 0) then Response.Redirect "thispage.asp"

  'Quantity input was good, show Procede page
%>
<html>
 <head>
   <title>Sample for scripter50</title>
 <head>
 <body>
  <form action="thispage.asp" method="post">
   <table>
<%
  For iCount to 1 to iQTY 
%>
    <tr>
     <td>
       First: <input type="text" name="first<%=iCount%>">
     </td>
     <td>
       This: <input type="text" name="This<%=iCount%>">
     </td>
     <td>
       That: <input type="text" name="That<%=iCount%>">
     </td>
     <td>
       The Other: <input type="text" name="TheOther<%=iCount%>">
     </tr>
    </tr>
<%
  Next
%>
    <tr>
     <td>
      <input type="hidden" name="qty" value="<%= cStr(iQTY)%>">
      <input type="submit" name="btnSubmit" value="Insert Records">
     </td>
    </tr>
   </table>
  </form>
 </body>
</html>
<%
  Response.End

elseif Request.Form("btnSubmit") = "Insert Records" then
  'Third time script is run, execute SQL here
  iQTY = CInt(Request.Form("qty"))

  For iCount to 1 to iQTY 
    sFirstName = Request.Form("first" & CStr(iCount))
    sThis = Request.Form("This" & CStr(iCount))
    sThat = Request.Form("That" & CStr(iCount))
    sTheOther = Request.Form("TheOther" & CStr(iCount))

    sql = "insert into table (Firstname, This, That, TheOther)  " _
        & "values('" & sFirstName & "'," _
        & sThis & ", " & sThat & ", " & TheOther
  
     objconn.execute sql
  Next
<%
<html>
 <head>
   <title>Sample for scripter50</title>
 <head>
 <body>
   Thank you.
   <br>
   <%= cStr(iQTY)%> records were inserted.
   <br>
   <a href="thispage.asp">Start Over</a>
 </body>
</html>
%>
End if
%>
 
ah, I forgot to add the closing parenthesis to the sql statement for the third time... There almost certainly other errors because i obviously didnt run the page because I dont have access to your database so just take that last bit of code as a sample/example of what you are trying to do.
 
Thanks Sheco for the great code response! But I'm a little lost on the "for" loop syntax. You showed "For iCount to 1 to iQTY" - not familiar with that - usually "For iCount = to something". What am I missing?


 
Oh the iCount is just the name of the variable that I used as the counter and the iQTY was the variable for how high to count.

You could just as well use i or x or foo:
[tt]
For i = 1 to iQTY
sFirstName = Request.Form("first" & CStr(i))


For x = 1 to iQTY
sFirstName = Request.Form("first" & CStr(x))


For foo = 1 to iQTY
sFirstName = Request.Form("first" & CStr(foo))
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top