面向对象的PHP和AJAX表单验证 [英] OO PHP and AJAX Form Validation

查看:129
本文介绍了面向对象的PHP和AJAX表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助,让JQuery的AJAX与方法在我的PHP类的一个工作。我验证使用的PHP类和它扩展数据库类的形式。我只是需要AJAX工作,我很好。你如何让AJAX与PHP的面向对象的工作?我一直停留在这个在过去的几天里,所有在互联网上已经研究并没有发现任何作品。可有人也许张贴越来越jQuery的AJAX功能与PHP的方法来工作的一个简单的例子?

I need some help with getting JQuery AJAX to work with a method in one of my PHP classes. I validated the form using a PHP class and which extends a Database class. I just need the AJAX to work and I'm good. How do you get AJAX to work with OO PHP? I have been stuck on this for the past few days and have researched all across the internet and have not found anything that works. Can someone maybe post a simple example of getting JQuery's AJAX function to work with a PHP method?

下面是返回PHP的方法是否用户通过验证或没有(contact.class.php延伸database.class.php):

Here is the PHP method that returns whether the user passed validation or not (contact.class.php which extends database.class.php):

public function isValidData() {

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

        return true;
    } else {
        return false;
    }

}

下面是Jquery的。阿贾克斯是在底部:

Here is the Jquery. The Ajax is at the bottom:

//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请求

//Ajax Request

功能submitForm(FORMDATA){

function 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的会禁用服务器验证?我还挺新的节目,我会AP preciate任何帮助,谢谢。

The Jquery seems to disable my PHP serverside validation too. When the JQuery is disabled, the server side validation works fine. Any idea why JQuery would disable the server validation? I am kinda new to programming and I would appreciate any help, thanks.

推荐答案

jQuery的是客户端验证它能够在不影响服务器端执行,问题是序列化的数据不获取发送到服务器,因此验证的服务器不是做,因为没有数据被到达那里。

the jquery is client side validation it cannot affect server side execution, the problem is the serialized data is not getting posted to the server hence the validation at server is not done because no data is reaching there.

为什么不u使用警报的每一步,看数据流。

why don't u use alert at every step to see the flow of data.

这篇关于面向对象的PHP和AJAX表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆