English 中文(简体)
OO PHP和AJAX表格验证
原标题:OO PHP and AJAX Form Validation

我需要一些帮助才能让 JQuery AJAX 在我的一个 PHP 课程中使用一种方法。 我使用 PHP 类验证了表格, 并扩展了一个数据库类 。 我只是需要 AJAX 来工作, 我很好。 您如何让 AJAX 和 OO PHP 一起工作? 过去几天我一直坚持着这个任务, 并且一直在互联网上研究, 没有找到任何有用的方法 。 有人能张贴一个让 JQuery s AJAX 函数使用 PHP 方法的简单例子吗?

这是 PHP 返回用户是否通过验证的方法( 连接. glass. php 扩展数据库. glass. php ) :

public function isValidData() {

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

        return true;
    } else {
        return false;
    }

}

这是杰奎里号 阿贾克斯号在下面

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

函数提交 Form( 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 是客户端验证, 它无法影响服务器侧面执行, 问题是序列数据没有被张贴到服务器上, 因此服务器上的验证没有完成, 因为没有数据到达服务器 。

为什么不在每一步都使用警报来观察数据的流动。

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签