简体   繁体   English

jQuery.validate无法通过Ajax处理帖子

[英]jQuery.validate not working with post via ajax

I'm little confused. 我有点困惑。 I build simple form: 我建立简单的形式:

<form id="simple-contact">
<input type="text" id="client-name" name="clientname" class="textF" placeholder="Imię i Nazwisko*"/>
<input type="text" id="client-mail" name="clientmail" class="emailF" placeholder="E-mail*"/>
<input type="text" id="client-phone" name="clientphone" class="textF" placeholder="Nr telefonu"/>
<textarea id="client-massege" name="clientmassage" class="textareaF" placeholder="Wpisz wiadomość"></textarea>
<input type="submit" id="client-wysylka" name="clientwysylka" value="Wyślij" class="btnF"/>
</form>

I use jQuery validation plugin and want submit form via ajax. 我使用jQuery验证插件,并希望通过Ajax提交表单。 So I wrote this part of code: 所以我写了这段代码:

<script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/jquery.validate.min.js"></script>    
<script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/additional-methods.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
    $('#simple-contact').validate({
     onfocusout: function (element) {
     $(element).valid();
     },
      rules: {
   clientname: "required",  
   clientmail: {                
      required: true,
      email: true
    },
      clientphone: {
      required: true,
      matches: "[0-9]",
      minlength:9,
      maxlength:9
    },
    clientmassage: {
          required: true
    }

},
    submitHandler: function(form) {
        request = $.ajax({
            type: "POST",
            cache: false,
            url: "http://www.wytworniaprojektu.eu/formularze/form-simple-wrapp-send.php",
            data: $(form).serialize(),
            timeout: 3000
            success: function() {alert('Done');},
            error: function() {alert('Error');}
    });

    return false;
    },
    messages: {
    clientname: "Podaj Imię i Nazwisko",
    clientmail: {
      required: 'Pole e-mail jest wymagane',
      email: 'To nie jest prawidłowy adres e-mail'
    },
    clientphone: {
      required: 'Musisz podać nr telefonu',
      matches: 'Numer musi zawierać 9 cyfr'
    },
    clientmassage: 'Wpisz wiadomość',
    },
    errorPlacement: function(error, element) {
    if(element.is(":radio") || element.is(":checkbox")) {
    error.appendTo(element.parent());
    }
    else {
    error.insertAfter(element);
    }
    },
   });
   });

I've also tried this solution https://stackoverflow.com/a/17627502/2801980 我也尝试过此解决方案https://stackoverflow.com/a/17627502/2801980

but still not working, i mean form not sending (guess not take code from ajax url (action path). I can't see any alert and submission redirect me to same page. 但仍然无法正常工作,我的意思是表单未发送(猜测未从ajax url(操作路径)获取代码。我看不到任何警报和提交将我重定向到同一页面。

See in action http://www.wytworniaprojektu.eu/#contact-form 观看实战http://www.wytworniaprojektu.eu/#contact-form

Well what happens here is your ajax works. 好吧,这里发生的是您的ajax作品。 It sends the request and receives the response. 它发送请求并接收响应。 I guess the way you have it set up right now is when you receive ANY response you redirect. 我想您现在设置的方式是当您收到任何重定向的响应时。 Problem is the response is an error. 问题是响应是错误。

What you should probably probably make an ajax request to your server-side, the server-side should to some task, and based on that task return response back to javascript. 您可能应该向服务器端发出ajax请求,服务器端应向某些任务发出请求,然后基于该任务将响应返回给javascript。

So lets say on your server-side you save the info to the database. 因此,假设您在服务器端将信息保存到数据库中。 If everything goes right you should return JSON for example { status : 1 }. 如果一切正常,则应返回JSON,例如{status:1}。 If there was any errors on the server side you might want to catch them and return {status : 0}. 如果服务器端发生任何错误,您可能希望捕获它们并返回{status:0}。

Now in your javascript 现在在您的JavaScript中

success: function(data) { 
   if(data.status) { 
         // do the redirect 
   } else { 
         // handle error 
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM