In many web applications, we will encounter many places where multiple rows of records need to be dynamically inserted. For example, on the talent website, when we fill in our resume, we need to fill in our project experience. We can dynamically add the number of items according to our actual situation. This is not added in the form of a separate page. This dynamic addition is in Add them dynamically to the same page, and finally submit them to the server and save them in the database.
In this article, I will use a similar example to dynamically add data items using Javascript in the frontend and save them to the database in the background.
Browser: IE.6.0
Backend: ASP (VBScript)
Frontend: HTML JavaScript
HTML code:
"
conn.execute(sql)
if request("ProjectName").count>0 then
dim maxid
maxid = 1
sql = "select max(id) as maxid from UserInfo"
set rs = conn.execute(sql)
maxid = rs("maxid")
rs.close
set rs = nothing
for i =1 to request("ProjectName").count
sql = "insert into ProjectInfo(uid,pname,pdesc,bdate,fdate) values("
sql=sql&""& maxid &","
sql=sql&"'"& request("ProjectName")(i) &"',"
sql=sql&"'"& request("Desc")(i) &"',"
sql =sql&"'"& request("BDate")(i) &"',"
sql=sql&"'"& request("FDate")(i) &"')"
Response.Write " "&sql&"
"
conn.execute(sql)
next
end if
if conn.Errors.count > 0 then ' If occus any error in the transaction , roll back transcation
conn.RollBackTrans
else ' If not error, commit the transcation
conn.commitTrans
end if
conn.close
set conn = nothing
%>
Picture 2: Add another row and fill in the data to Figure 3
Figure 3: After adding two rows of data, click the Submit button to submit the data
Figure 4: After submitting the form, the database will execute several SQL statements as printed by the browser, and the data will be successfully added to the database.
Summary:
This article describes how to use Javascript to dynamically add columns for user input data in the frontend, and use ASP technology in the backend to insert the data added in the frontend into the database.
I hope it will be helpful to those who are learning ASP and Javascript.
If you have any questions, you can contact me. If you have any comments on this article, you are warmly welcome to criticize and correct me!