Javascript 如何在使用jQuery提交表单时完成$.get()

Javascript 如何在使用jQuery提交表单时完成$.get(),javascript,jquery,ajax,get,form-submit,Javascript,Jquery,Ajax,Get,Form Submit,我正在使用第三方购物车,该购物车将注册表发送到.cgi脚本 我想通过jQuery$.get()函数调用将该表单中的信息发送给我和客户 $.get调用registration.mail.php脚本 问题是表单提交似乎在ajax调用完成之前就取消了ajax调用 我可以想出一些不雅观的解决方案,我决定,因为我即将旅行,使用计数器,让用户点击“注册”两次。这个解决方案显然伤害了我的灵魂 下面是位于页脚中的Javascript: <script type="text/javascript">

我正在使用第三方购物车,该购物车将注册表发送到.cgi脚本

我想通过jQuery$.get()函数调用将该表单中的信息发送给我和客户

$.get调用registration.mail.php脚本

问题是表单提交似乎在ajax调用完成之前就取消了ajax调用

我可以想出一些不雅观的解决方案,我决定,因为我即将旅行,使用计数器,让用户点击“注册”两次。这个解决方案显然伤害了我的灵魂

下面是位于页脚中的Javascript:

<script type="text/javascript">
var getsuccess = false;
$(document).ready(function() {

    $('form#registerWholesale').bind('submit', function() {

        $email = $('#email').val();
        $username = $('#contactname').val();
        $company = $('#company').val();
        $phone = $('#billphone1').val();

        $message = "A new customer has registered at the wholesale website. \n ";
        $message += "They have the username of: " + $username + ". \n";
        $message += "Their company is: " + $company + ". \n";
        $message += "Their email is: " + $email + ". \n";
        $message += "Their phone is: " + $phone + ". \n";
        $message += "Please help them ASAP. You can access the back end of the site at: http://location of website admin backend";

      $.get('/mail.php', {from: 'orders@OurCompany.com', message: $message, email: $email, username: $username, company: $company}, function(data, textStatus, xhr) {
         getsuccess = true;
      });

      if (getsuccess) {
        return true;
      }else{
        return false;
      }
    });


</script>

var getsuccess=false;
$(文档).ready(函数(){
$('form#registerholesale').bind('submit',function(){
$email=$('#email').val();
$username=$('#contactname').val();
$company=$(“#company”).val();
$phone=$('#billphone1').val();
$message=“新客户已在批发网站注册。\n”;
$message+=”他们的用户名为:“+$username+”\n”;
$message+=“他们的公司是:“+$company+”\n”;
$message+=”他们的电子邮件是:“+$email+”\n”;
$message+=”他们的手机是:“+$phone+”\n”;
$message+=“请尽快帮助他们。您可以访问网站的后端,网址为:http://location 网站管理后端的管理”;
$.get('/mail.php',{from:'orders@OurCompany.com',message:$message,email:$email,username:$username,company:$company},函数(数据,文本状态,xhr){
getsuccess=true;
});
如果(获取成功){
返回true;
}否则{
返回false;
}
});
下面是registration.mail.php代码

<?php

//Vendor email (to us)
$to = "ouremail@OurCompany.com";
$subject = "URGENT--New Registration--URGENT";
$message = htmlentities($_GET['message'], ENT_QUOTES, 'UTF-8');
//$message = $_GET['message'];
$from = htmlentities($_GET['from'], ENT_QUOTES, 'UTF-8');
//$from = trim($_GET['from']);
$headers = "From: $from";
mail($to,$subject,$message,$headers);
//echo "Mail Sent.";



//Customer email (to them)
$to_cust = htmlentities($_GET['email'], ENT_QUOTES, 'UTF-8');
$subject_cust = 'OurCompany Online Wholesale Account Request Recieved';
$message_cust = "Thank you for you interest in OurCompany's new wholesale website. \n\n
We have received your request to create an account.   In order to obtain a wholesale account, a OurCompany representative must verify your account information and details by telephone.  OurCompany will contact you within 1 business day at the telephone number that we have on file, or feel free to give us a call at 1-xxx-xxx-xxxx anytime if you would like a more rapid approval.  \n\n

Thanks Again ~ OurCompany";

$headers_cust = "From: orders@OurCompany.com";
mail($to_cust,$subject_cust,$message_cust,$headers_cust)
?> 

一种解决方案是将事件绑定到按钮单击,阻止单击的默认操作,使表单不提交,然后最终在.get回调中提交表单

注意:正如Jaanus建议的那样,你真的应该看看应用程序的设计,在同一个调用中发送邮件并保存购物车。如果发送了电子邮件,那么表单提交操作失败,会发生什么

$(document).ready(function() {

    $('#yourSubmitButton').click( function(ev) {

        //prevent form submission
        ev.preventDefault();

        $email = $('#email').val();
        $username = $('#contactname').val();
        $company = $('#company').val();
        $phone = $('#billphone1').val();

        $message = "A new customer has registered at the wholesale website. \n ";
        $message += "They have the username of: " + $username + ". \n";
        $message += "Their company is: " + $company + ". \n";
        $message += "Their email is: " + $email + ". \n";
        $message += "Their phone is: " + $phone + ". \n";
        $message += "Please help them ASAP. You can access the back end of the site at: http://location of website admin backend";

        $.get('/mail.php', {from: 'orders@OurCompany.com', message: $message, email: $email, username: $username, company: $company}, function(data, textStatus, xhr) {

           //all is well, submit the form
           $('#yourForm').submit()

       });

    });

一种解决方案是将事件绑定到按钮单击,阻止单击的默认操作,使表单不提交,然后最终在.get回调中提交表单

注意:正如Jaanus建议的那样,你真的应该看看应用程序的设计,在同一个调用中发送邮件并保存购物车。如果发送了电子邮件,那么表单提交操作失败,会发生什么

$(document).ready(function() {

    $('#yourSubmitButton').click( function(ev) {

        //prevent form submission
        ev.preventDefault();

        $email = $('#email').val();
        $username = $('#contactname').val();
        $company = $('#company').val();
        $phone = $('#billphone1').val();

        $message = "A new customer has registered at the wholesale website. \n ";
        $message += "They have the username of: " + $username + ". \n";
        $message += "Their company is: " + $company + ". \n";
        $message += "Their email is: " + $email + ". \n";
        $message += "Their phone is: " + $phone + ". \n";
        $message += "Please help them ASAP. You can access the back end of the site at: http://location of website admin backend";

        $.get('/mail.php', {from: 'orders@OurCompany.com', message: $message, email: $email, username: $username, company: $company}, function(data, textStatus, xhr) {

           //all is well, submit the form
           $('#yourForm').submit()

       });

    });

您是否尝试过将ajax调用放在函数中,并将表单的onSubmit挂起

onsubmit="myajaxcallFunc(); return true;"

这通常用于停止提交,返回值为false,但在这种情况下,它可能会执行您想要的操作。

您是否尝试过将ajax调用放入函数中,并钩住表单的onSubmit

onsubmit="myajaxcallFunc(); return true;"

这通常用于停止提交,返回值为false,但在这种情况下,它可能会执行您想要的操作。

您的Ajax get处理程序设置一个异步回调。换句话说,这段代码:

  $.get('/mail.php', {from: 'orders@OurCompany.com', message: $message, email: $email, username: $username, company: $company}, function(data, textStatus, xhr) {
     getsuccess = true; <---- THIS
  });
Ajax调用从未完成,它总是返回false

基本上,您应该决定是否要使用Ajax提交表单,并且只使用其中一种。如果要使用Ajax,提交事件处理程序应该始终返回False


编辑:我没有意识到邮件发送和表单提交是两个不同的操作,两个独立的服务器调用。(你没有显示应用程序的其余部分,但这是我从其他答案中得到的想法。)这是一种糟糕的设计,可能会导致不一致的结果和糟糕的数据。您应该重新设计应用程序,以便在服务器端的同一位置处理这两件事,以便web应用程序只进行一次调用,无论这一次调用是通过Ajax还是通过常规表单提交进行的。

您的Ajax get处理程序设置了一个异步回调。在其他情况下文字,这段代码:

  $.get('/mail.php', {from: 'orders@OurCompany.com', message: $message, email: $email, username: $username, company: $company}, function(data, textStatus, xhr) {
     getsuccess = true; <---- THIS
  });
Ajax调用从未完成,它总是返回false

基本上,您应该决定是否要使用Ajax提交表单,并且只使用其中一种。如果要使用Ajax,提交事件处理程序应该始终返回False


编辑:我没有意识到邮件发送和表单提交是两个不同的操作,两个独立的服务器调用。(你没有显示应用程序的其余部分,但这是我从其他答案中得到的想法。)这是一个糟糕的设计,可能会导致不一致的结果和糟糕的数据。您应该重新设计应用程序,以便在服务器端的同一位置处理这两件事,以便web应用程序只进行一次调用,无论这一次调用是通过Ajax还是通过常规表单提交进行的。

您传递给
$的回调将被评估由于AJAX是异步的,您的代码将同时继续求值,这意味着您的if条件和两个return语句将始终在调用AJAX回调之前求值

您要做的是让
submit
侦听器始终返回false,并在AJAX回调中有条件地触发
$('form#registerholesale')。submit();


当然,这里也有一个设计考虑:您可能希望电子邮件发送和批发注册是原子的。如果邮件发送,您总是希望表单提交中发生的事情也发生,对吗?在这种情况下,您希望将电子邮件发送移到表单回发,或者将批发注册移到aJAX回调,因此它全部在一个步骤中处理。

传递给
$.get
的回调将在AJAX请求之后进行计算。由于AJAX是异步的,因此您的代码将在同一时间继续计算,这意味着在调用AJAX回调之前,始终会计算带有两个返回语句的if条件

您要做的是将
submit
侦听器提交给alwa