如何通过JQuery Ajax调用在PHP中验证我的表单?

时间:2014-06-18 15:50:36

标签: javascript php jquery ajax

如何在不收到main.js中ajax定义中定义的错误消息的情况下在PHP中验证我的代码?

注意:Chrome控制台返回: XMLHttpRequest无法加载file:/// C:/Documents/Mini%20Revision%20Projects/Project%20Website%203/ajax.php。收到无效回复。因此,不允许原点'null'访问。

以下是我的代码:

main.html中

<!DOCTYPE HTML>
<html>
    <head>  
        <script type="text/javascript" src="C:\Documents\jQuery\jquery2.js"></script>
    </head> 

    <body>

        <ul id="info1">
                <li>Put anything in the field below.</li>
        </ul>
        <form id="form1">
                <input type="text" name="field1" id="field1">
                <input type="submit" name="submit" id="submit" value="Submit Form">
        </form>

        <script type="text/javascript" src="main.js"></script>  

    </body>

</html>

main.js

$(document).ready(function() {

    $("#form1").submit(function( event ) {
        event.preventDefault();
        //alert("happy");
        $.ajax({
            type: 'POST',
            url: 'ajax.php',
            data: $(this).serialize(),
            dataType: 'json',
            success: function (data) {
                console.log(data);
                $("#info1").html(data.msg);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("Status: " + textStatus);
                alert("Error: " + errorThrown);
            }
        });
    });
});

ajax.php

<?php
    class ajaxValidate {

            function formValidate() {
                    //Put form elements into post variables (this is where you would sanitize your data)
                    $field1 = @$_POST['field1'];

                    //Establish values that will be returned via ajax
                    $return = array();
                    $return['msg'] = '';
                    $return['error'] = false;

                    //Begin form validation functionality
                    if (!isset($field1) || empty($field1)){
                            $return['error'] = true;
                            $return['msg'] .= '<li>Error: Field1 is empty.</li>';
                    }

                    //Begin form success functionality
                    if ($return['error'] === false){
                            $return['msg'] = '<li>Success Message</li>';
                    }

                    //Return json encoded results
                    return json_encode($return);
            }

    }

    $ajaxValidate = new ajaxValidate;
    echo $ajaxValidate->formValidate();
?>

2 个答案:

答案 0 :(得分:1)

首先,验证PHP是否未返回警告或严重错误。将此代码插入代码顶部。如果PHP返回隐藏错误,则成功数据值将为null。

ini_set("display_errors", "On");
error_reporting(E_ALL);

PHP Error reporting manual

PHP run-time display_error

我认为如果你在php中返回一个JSON数组,你需要调用一个数组。

$("#info1").html(data['msg']);

尝试返回post值以验证$ _POST是否为空:

return json_encode($_POST);

如果直接定义第一行,则无需定义空数组。

//$return = array();
$return['msg'] = '';

答案 1 :(得分:0)

您需要拥有PHP服务器才能使PHP脚本正常工作。安装类似XAMPP(windows)或MAMP(linux)的东西