php - OO PHP 和 AJAX 表单验证

标签 php jquery mysql ajax validation

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

这里是返回用户是否通过验证的 PHP 方法(contact.class.php 扩展了database.class.php):

public function isValidData() {

if ($this -> firstName() && $this -> lastName() && $this -> email() && $this -> subject() && $this -> message()) {

        return true;
    } else {
        return false;
    }

}

这是 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;
    }

}

 });

//Ajax请求

函数submitForm(formData){

$.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);

    }
});

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

最佳答案

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

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

关于php - OO PHP 和 AJAX 表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10740622/

相关文章:

php - 如何使用 shell 和终端克隆 mysql 数据库而不进行 mysql 转储和上传

php - 读取文件的元数据

php - 防止php使用默认用户名和密码到mysql

javascript - 函数和自调用匿名函数之间的 jQuery 区别

javascript - 对于类中的每个元素,console.log 其值

mysql - 根据一个值过滤所有行

php - 无法使用 PHP 和 MySQL 获取已接受的请求

php - 检查类是否在 PHP 中有方法

mysql - 如何在 SQL 中获取所选计数的最小值

javascript - 自定义 CSS 导航下拉菜单