Ajax-email已经存在.仍然是形式提交.(


ajax-email already exist..still form is submitting. :(

我想阻止表单提交…如果电子邮件已经存在…我使用了隐藏变量"我的代码. .

$.ajax({
     url: link_url,
    type: "post", //post is more secure
    data: JSON.stringify(senddata), // JSON is format
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}, // for @ space and all
    success: function(result){
        if(result > 0){
            document.getElementById("email_valid").className = "error";
            document.getElementById('email_valid').innerHTML='Email already exist.';
            var res=document.getElementById("dnsub").value;
            if(res>0) {
                flag=false;
            }
            else{flag=true;}
        }
        else{
            document.getElementById("email_valid").className = "success";
            document.getElementById('email_valid').innerHTML='Available';
        }
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
      }
})

是我的ajax代码如何使用隐藏变量

根据我的理解,您正在检查电子邮件地址是否存在于表单提交中。

将输入类型从"Submit"改为"button"。

对于该按钮,写一个事件为on click。在"Onclick"按钮中检查电子邮件地址是否存在。

如果存在则向用户显示错误消息,否则触发表单提交事件

$("form").submit();

您必须首先阻止默认表单提交。然后在ajax调用的success处理程序中提交表单。

$('#form_id').submit(function(e){
  e.preventDefault(); // this will prevent form submission
  $.ajax({
     url: link_url,
     type: "post", //post is more secure
     data: JSON.stringify(senddata), // JSON is format
     headers: {'Content-Type': 'application/x-www-form-urlencoded'}, // for @ space and all
     context: this, // pointing to form submit
     success: function(result){
        if(result > 0){
            document.getElementById("email_valid").className = "error";
            document.getElementById('email_valid').innerHTML='Email already exist.';
            var res=document.getElementById("dnsub").value;   
        }
        else{
            document.getElementById("email_valid").className = "success";
            document.getElementById('email_valid').innerHTML='Available';
            // the above two lines will be moot as the form will submit and page will get refreshed
            this.submit(); // all good, submit the form
        }
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
      }
  });
});