Stop ASP script auto-running on page load

天大地大妈咪最大 提交于 2021-01-20 12:46:52

问题


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>

回答1:


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


来源:https://stackoverflow.com/questions/19505098/stop-asp-script-auto-running-on-page-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!