停止ASP脚本在页面加载时自动运行 [英] Stop ASP script auto-running on page load
问题描述
我在这里遇到问题.我已经创建了一个页面,用于向数据库中添加记录,它工作正常,但是每次页面加载时都会运行asp脚本,每次页面加载时都会向数据库中输入空白记录,这很烦人我有其他脚本.我觉得我很愚蠢,但我所需要的只是让脚本仅在单击提交"按钮后才能运行,我如何才能做到这一点?
I'm having a problem here. I've created a page for adding records into a database, it's working fine however the asp script is running every time the page loads, inputting a blank record to the database each time the page is loaded, this is very annoying as it messes with other scripts I have. I feel I am being very stupid but all I need is for the script to run only once the submit button has been clicked, how do I get it to do this?
<!DOCTYPE html>
<html>
<title>
Teacher Registration
</title>
<body>
<h1>
Teacher registration
</h1>
<form name="teacherReg" action="Registration.asp" method="POST">
First name:<input type="text" name="firstname"><br>
Last name:<input type="text" name="lastname"><br>
Password :<input type="password" name="password">
<input type="submit" value="submit">
</form>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Open ="Driver={SQL Server}; Server=QuizDynamics.db.11989315.hostedresource.com; Database=QuizDynamics; Uid=QuizDynamics; Pwd=Compostheap12!;"
set rs=Server.CreateObject("ADODB.recordset")
rs.Open "Select * from teachers", conn
sql="INSERT INTO teachers (firstname, password, lastname)"
sql=sql & " VALUES "
sql=sql & "('" & Request.Form("firstname") & "',"
sql=sql & "'" & Request.Form("password") & "',"
sql=sql & "'" & Request.Form("lastname") & "')"
on error resume next
conn.Execute sql,recaffected
if err<>0 then
Response.Write("No update permissions!")
else
Response.Write("<h3>" & recaffected & " record added</h3>")
end if
conn.close
%>
</body>
</html>
推荐答案
为您的submit输入提供一个名称属性-例如submitbutton-然后执行类似
Give your submit input a name attribute - eg submitbutton - then do something like
if request.form("submitbutton") <> "" then
'put your insert code here
End if
这篇关于停止ASP脚本在页面加载时自动运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!