Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OO-PHP和AJAX表单验证_Php_Jquery_Mysql_Ajax_Validation - Fatal编程技术网

OO-PHP和AJAX表单验证

OO-PHP和AJAX表单验证,php,jquery,mysql,ajax,validation,Php,Jquery,Mysql,Ajax,Validation,我需要一些帮助,让jqueryajax在我的一个PHP类中使用一个方法。我使用一个PHP类验证了表单,该类扩展了一个数据库类。我只是需要AJAX来工作,我很好。如何让AJAX与OO PHP协同工作?在过去的几天里,我一直在研究这个问题,并在互联网上进行了研究,但没有发现任何有效的方法。有人可以发布一个简单的例子,让JQuery的AJAX函数与PHP方法一起工作吗 下面是返回用户是否通过验证的PHP方法(contact.class.PHP,它扩展了database.class.PHP): 这是Jq

我需要一些帮助,让jqueryajax在我的一个PHP类中使用一个方法。我使用一个PHP类验证了表单,该类扩展了一个数据库类。我只是需要AJAX来工作,我很好。如何让AJAX与OO PHP协同工作?在过去的几天里,我一直在研究这个问题,并在互联网上进行了研究,但没有发现任何有效的方法。有人可以发布一个简单的例子,让JQuery的AJAX函数与PHP方法一起工作吗

下面是返回用户是否通过验证的PHP方法(contact.class.PHP,它扩展了database.class.PHP):

这是Jquery。Ajax位于底部:

//Submit function called when the user clicks the submit button

$('#contact_form').submit(function(e) {

    //Prevent submission until the user passes validation
    e.preventDefault();

    //If all the functions return true, then send form to the AJAX function
    if(validFirstName() && validLastName() && validEmail() && validSubject() && validMessage()) {
        //Serialize the data in the form for the AJAX Request
        var formData = $('#contact_form').serialize();

        //submitForm(formData);
        //Displays success message, clears contact form and hides the lightbox
        $('#contact_form').fadeOut(1000, function() {
            $('.success').html('Form submission successful.' + '<br/>' + 'Thank you ' + $('input.first').val() + "!").fadeIn(4000, function() {
                //Clears contact form
                $('.first').val('');
                $('.last').val('');
                $('.email').val('');
                $('.subject').val('');
                $('.message').val('');
                //Hides success message
                $('.success').hide();
                //Hides lightbox
                $('.mask, .main_contact').css('display', 'none');
            });

        });
        return true;

    } else {
        return false;
    }

});

//Validates the user's first name
function validFirstName() {
    var firstName = $('.first').val();
    if(firstName.length <= 2 || firstName == '') {
        $('.error').show().html('3 Characters required!<br/>');
        $('.first').css('box-shadow', ' 0 0 10px #B40404');
        return false;
    } else {
        $('.first').css('box-shadow', '0 0 4px #000');
        $('.error').hide();
        return true;
    }

}

//Validates the user's last name
function validLastName() {
    var lastName = $('input.last').val();
    if(lastName.length <= 2 || lastName == '') {
        $('.error').show().html('3 Characters required!<br/>');
        $('input.last').css('box-shadow', '0 0 10px #B40404');
        return false;

    } else {
        $('input.last').css('box-shadow', '0 0 4px #000');
        $('.error').hide();
        return true;
    }
}

//Validates the user's email
function validEmail() {
    var email = $('.email').val();
    if(!email.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)) {
        $('.error').show().html('Invalid email address!<br/>');
        $('.email').css('box-shadow', ' 0 0 10px #B40404');
        return false;

    } else {
        $('.email').css('box-shadow', '0 0 4px #000');
        $('.error').hide();
        return true;
    }

}

//Validate the subject input in the contact form
function validSubject() {
    var subject = $('.subject').val();
    if(subject.length <= 2 || subject == '') {
        $('.error').show().html('3 Characters required!<br/>');
        $('.subject').css('box-shadow', ' 0 0 10px #B40404');
        return false;
    } else {
        $('.subject').css('box-shadow', '0 0 4px #000');
        $('.error').hide();
        return true;
    }
}

//Validate the message input
function validMessage() {

    var message = $('.message').val();
    if(message.length <= 2 || message == '') {
        $('.error').show().html('3 Characters required!<br/>');
        $('.message').css('box-shadow', ' 0 0 10px #B40404');
        return false;
    } else {
        $('.message').css('box-shadow', '0 0 4px #000');
        $('.error').hide();
        return true;
    }

}

 });

Jquery似乎也禁用了我的PHP服务器端验证。当JQuery被禁用时,服务器端验证工作正常。知道JQuery为什么会禁用服务器验证吗?我对编程有点陌生,如果有任何帮助,我将不胜感激,谢谢

jquery是客户端验证,它不会影响服务器端执行,问题是序列化数据没有发布到服务器,因此服务器上的验证没有完成,因为没有数据到达服务器

为什么不在每一步都使用警报来查看数据流呢

//Submit function called when the user clicks the submit button

$('#contact_form').submit(function(e) {

    //Prevent submission until the user passes validation
    e.preventDefault();

    //If all the functions return true, then send form to the AJAX function
    if(validFirstName() && validLastName() && validEmail() && validSubject() && validMessage()) {
        //Serialize the data in the form for the AJAX Request
        var formData = $('#contact_form').serialize();

        //submitForm(formData);
        //Displays success message, clears contact form and hides the lightbox
        $('#contact_form').fadeOut(1000, function() {
            $('.success').html('Form submission successful.' + '<br/>' + 'Thank you ' + $('input.first').val() + "!").fadeIn(4000, function() {
                //Clears contact form
                $('.first').val('');
                $('.last').val('');
                $('.email').val('');
                $('.subject').val('');
                $('.message').val('');
                //Hides success message
                $('.success').hide();
                //Hides lightbox
                $('.mask, .main_contact').css('display', 'none');
            });

        });
        return true;

    } else {
        return false;
    }

});

//Validates the user's first name
function validFirstName() {
    var firstName = $('.first').val();
    if(firstName.length <= 2 || firstName == '') {
        $('.error').show().html('3 Characters required!<br/>');
        $('.first').css('box-shadow', ' 0 0 10px #B40404');
        return false;
    } else {
        $('.first').css('box-shadow', '0 0 4px #000');
        $('.error').hide();
        return true;
    }

}

//Validates the user's last name
function validLastName() {
    var lastName = $('input.last').val();
    if(lastName.length <= 2 || lastName == '') {
        $('.error').show().html('3 Characters required!<br/>');
        $('input.last').css('box-shadow', '0 0 10px #B40404');
        return false;

    } else {
        $('input.last').css('box-shadow', '0 0 4px #000');
        $('.error').hide();
        return true;
    }
}

//Validates the user's email
function validEmail() {
    var email = $('.email').val();
    if(!email.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)) {
        $('.error').show().html('Invalid email address!<br/>');
        $('.email').css('box-shadow', ' 0 0 10px #B40404');
        return false;

    } else {
        $('.email').css('box-shadow', '0 0 4px #000');
        $('.error').hide();
        return true;
    }

}

//Validate the subject input in the contact form
function validSubject() {
    var subject = $('.subject').val();
    if(subject.length <= 2 || subject == '') {
        $('.error').show().html('3 Characters required!<br/>');
        $('.subject').css('box-shadow', ' 0 0 10px #B40404');
        return false;
    } else {
        $('.subject').css('box-shadow', '0 0 4px #000');
        $('.error').hide();
        return true;
    }
}

//Validate the message input
function validMessage() {

    var message = $('.message').val();
    if(message.length <= 2 || message == '') {
        $('.error').show().html('3 Characters required!<br/>');
        $('.message').css('box-shadow', ' 0 0 10px #B40404');
        return false;
    } else {
        $('.message').css('box-shadow', '0 0 4px #000');
        $('.error').hide();
        return true;
    }

}

 });
$.ajax({
    type : "POST",
    url : "includes/function.php",
    data : formData,
    dataType: 'json',
    cache : false,
    success : function(formData) {
        if(formData.success) {

            alert(formData.msg);
        } else {
            alert("Error");
        }
        console.log(formData);

    }
});