简体   繁体   English

jQuery验证+ Ajax + PHP +无需页面刷新

[英]Jquery validation + ajax + php + without page refresh

I have been suffering from a solution of a problem for days. 几天来我一直在解决问题。 I would like to use jquery validation and ajax call without page refresh together. 我想在不一起刷新页面的情况下使用jquery验证和ajax调用。 If I only use one of them, the code is working properly but together not. 如果仅使用其中之一,则代码可以正常工作,但不能一起工作。 I would like a very simple, comprehensible code. 我想要一个非常简单,可理解的代码。

Could you help me where the problem can be? 您能帮我解决问题所在吗? Thanks in advance. 提前致谢。

Best regards, Atti 最好的问候,阿蒂

**teszt.php**
<!doctype html>
<html lang="hu">
<head>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jquery.validate.js"></script>
<script>

$(document).ready(function(){

        $('#form').validate({
            rules: {
                nev: {
                  required: true,
                  minlength: 2
                }
            },
            messages: {
                nev: {
                  required: "We need your email address to contact you",
                  minlength: 2
                }
            },
            submitHandler: function(form) {
                $('#button_id').click(function(){
                    var nev=$("#nev").val();
                    var action=$("#action").val();

                    $.ajax({
                        type: "post",
                        url: "php.php",
                        data: "nev="+nev+"&action="+action,
                        success:function(result){
                            $("#nevem").html(result);
                        }
                    });
                });
                form.submit();          
            }
        });
    });


</script>
</head>
<body>
<form id="form">
<input type="text" id="nev" name="nev">
<input type="hidden" id="action" name="action" value="addadd">
<input type="button" id="button_id" name="button_id" value="Mentés">
</form>
<div id="nevem"></div>

</body>
</html>

**php.php**
<?php 

if ($_POST["action"] == "addadd") {

echo $_POST["nev"];
} else {
    echo 987;
}
?>

Call it as such: 这样称呼它:

$('#button_id').click(function(){
$("#myform").submit(function(e) {
  e.preventDefault();
}).validate({
  rules: {
            nev: {
              required: true,
              minlength: 2
            }
        },
        messages: {
            nev: {
              required: "We need your email address to contact you",
              minlength: 2
            }
        },
        submitHandler: function(form) {

                var nev=$("#nev").val();
                var action=$("#action").val();

                $.ajax({
                    type: "post",
                    url: "php.php",
                    data: "nev="+nev+"&action="+action,
                    success:function(result){
                        $("#nevem").html(result);
                    }
                });
            });
}); 
}); // no .submit() here

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

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