经典ASP - 使用Ajax提交查询表单而无需刷新

时间:2022-11-23 19:39:27

I've had a look through Google etc but can't find anything for what I'm after so asking my fellow SE members for a little help.

我已经看了谷歌等,但找不到任何我想要的东西,所以请求我的SE成员一点帮助。

I have a form which is contained on my page and is consistent throughout the site. I would like to submit the enquiry form without the page refreshing which is the normal route with Classic ASP.

我的表单包含在我的页面上,并且在整个站点中都是一致的。我想提交没有页面刷新的查询表格,这是经典ASP的正常路线。

I have a separate ASP page that contains my mail script and that works fine and sends the email, however I would like to use Ajax to call that page, send the email and then inform the user that its been sent.

我有一个单独的ASP页面,其中包含我的邮件脚本,并且工作正常并发送电子邮件,但是我想使用Ajax来调用该页面,发送电子邮件然后通知用户它已被发送。

Any help would be appreciated.

任何帮助,将不胜感激。

1 个解决方案

#1


8  

Classic ASP, ASP.NET, PHP or any other dynamc language, its all the same as what you're really doing is FORM POST ing, and that's HTML, not actually ASP.

经典的ASP,ASP.NET,PHP或任何其他动力语言,它与你真正做的一样是FORM POST ing,那是HTML,而不是ASP。

Assuming that you have a form like

假设你有一个像这样的表格

<form id="frm-submit" action="/SendEmail.asp" action="POST">

   ...

   <input type="submit" value="Submit form" />
</form>

add a little jQuery to the page as

添加一个小jQuery到页面

$(function() {

   $("#frm-send").submit(function() {
      var data = $(this).serialize(),
          action = $(this).attr("action"),
          method = $(this).attr("method");

      $(".loading").show(); // show loading div
      $.ajax({
         url: action,
         type: method,
         data: data,
         success: function(data) {
             // all went well...
         },
         error: function(err) {
             // there was something not right...
         },
         complete: function() {
            $(".loading").hide(); // hide the loading
         }
      });

      return false; // don't let the form be submitted
   });

});

and you're ready to go.

你已经准备好了。


in your sendMail.asp page, everything you send to Response.Write will be in the data variable of the success method, so after you send the email you have:

在sendMail.asp页面中,您发送到Response.Write的所有内容都将在success方法的数据变量中,因此在您发送电子邮件后,您将拥有:

Response.Write("done")
Responde.Flush()
Response.End()

in the javascript part, you can handle it like:

在javascript部分,你可以像下面这样处理:

success(data) {
    if(data === "done")
    {
       // Super duper! let's show the user a Thank you for submit page
       document.location = "/ThankYou.asp";
    }
}

#1


8  

Classic ASP, ASP.NET, PHP or any other dynamc language, its all the same as what you're really doing is FORM POST ing, and that's HTML, not actually ASP.

经典的ASP,ASP.NET,PHP或任何其他动力语言,它与你真正做的一样是FORM POST ing,那是HTML,而不是ASP。

Assuming that you have a form like

假设你有一个像这样的表格

<form id="frm-submit" action="/SendEmail.asp" action="POST">

   ...

   <input type="submit" value="Submit form" />
</form>

add a little jQuery to the page as

添加一个小jQuery到页面

$(function() {

   $("#frm-send").submit(function() {
      var data = $(this).serialize(),
          action = $(this).attr("action"),
          method = $(this).attr("method");

      $(".loading").show(); // show loading div
      $.ajax({
         url: action,
         type: method,
         data: data,
         success: function(data) {
             // all went well...
         },
         error: function(err) {
             // there was something not right...
         },
         complete: function() {
            $(".loading").hide(); // hide the loading
         }
      });

      return false; // don't let the form be submitted
   });

});

and you're ready to go.

你已经准备好了。


in your sendMail.asp page, everything you send to Response.Write will be in the data variable of the success method, so after you send the email you have:

在sendMail.asp页面中,您发送到Response.Write的所有内容都将在success方法的数据变量中,因此在您发送电子邮件后,您将拥有:

Response.Write("done")
Responde.Flush()
Response.End()

in the javascript part, you can handle it like:

在javascript部分,你可以像下面这样处理:

success(data) {
    if(data === "done")
    {
       // Super duper! let's show the user a Thank you for submit page
       document.location = "/ThankYou.asp";
    }
}