在向PHP发送POST后,Ajax没有收到回复

时间:2016-12-20 00:23:41

标签: php ajax ajaxform

我在网站上建立一个简单的注册表单。 我通过AJAX发送数据,无法得到请求的肯定或否定答案。 如果我直接发送到PHP文件,一切正常,如果使用AJAX,AFTERSUCCESS不做任何事情。 我使用Jquery和JQUERYFORMS

的index.php:

<form class="uk-form" id="register_frm" action="php/register.php" method="POST" enctype="multipart/form-data">
    <input type="email" name="email" id="email" class="uk-width-1-1 uk-form-large" value="aaa@eee.vv">
    <input value="111" type="password" name="pass" id="pass" class="uk-width-1-1 uk-form-large" >
    <input value="111" type="password" name="pass2" id="pass2" class="uk-width-1-1 uk-form-large">
    <button class="uk-button uk-button-primary uk-button-large uk-align-center" type="submit" name="submit_btn" id="submit_btn" value="reg">Register <i class="uk-icon uk-icon-spin uk-icon-spinner" id="output"></i></button>
    <div id="output"></div>
</form>

使用Javascript:

$(document).ready(function() {
    $("#register_frm").submit(function(event){
        var options = { 
            target:   '#output',   // target element(s) to be updated with server response 
            beforeSubmit:  beforeSubmit,  // pre-submit callback 
            success:       afterSuccess,  // post-submit callback 
            uploadProgress: OnProgress, //upload progress callback
            dataType    : 'json',
            resetForm: false        // reset the form after successful submit 
        }; 

        $('#register_frm').ajaxSubmit(options);            
        // always return false to prevent standard browser submit and page navigation 
        return false;

        function afterSuccess(data)
        {
            console.log(data);
        }

        function beforeSubmit(){
        }

        function OnProgress(event, position, total, percentComplete)
        {
            //Progress bar
        }

        function bytesToSize(bytes) {
            var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
            if (bytes == 0) return '0 Bytes';
            var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
            return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
        }
    });
}); 

PHP

if (isset($_POST['submit_btn'])&& $_POST['submit_btn']== 'reg') {
    $data = array();
    $errors = array();

    $email=trim($_POST['email']);
    $pass=trim($_POST['pass']);
    $pass2=trim($_POST['pass2']);

    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors['email']="Invalid email";
    }

    if (empty($pass) || empty($pass2)) {
        $errors['pass']="Invalid passwords";
    }

    if (strcmp($pass, $pass2)) {
        $errors['pass']="Invalid passwords";
    }

    if (empty($errors)) {
        $add_user= add_new_user($conn,$email,$pass);
    }

    if ($add_user) {
        $data['success'] = true;
    } else {
        $data['success'] = false;
    }

    echo json_encode($data);
}

1 个答案:

答案 0 :(得分:0)

好吧,我发现了问题。 在PHP中接收数据我检查是否按下了SUBMIT按钮,然后输入VALUE。 AJAX没有发送按钮,所以我添加了一个HIDDEN字段并检查我是否得到了正确的表单。 谢谢大家

<form class="uk-form" id="register_frm" action="php/register.php" method="POST" enctype="multipart/form-data">
<input type="email" name="email" id="email" class="uk-width-1-1 uk-form-large" value="aaa@eee.vv">
<input value="111" type="password" name="pass" id="pass" class="uk-width-1-1 uk-form-large" >
<input value="111" type="password" name="pass2" id="pass2" class="uk-width-1-1 uk-form-large">
<input type="hidden" name="reg" id="reg">
<button class="uk-button uk-button-primary uk-button-large uk-align-center" type="submit" name="submit_btn" id="submit_btn" value="reg">Register <i class="uk-icon uk-icon-spin uk-icon-spinner" id="output"></i></button>
<div id="output"></div>

PHP

if (isset($_POST['reg'])) {}