Ajax成功与错误

时间:2014-07-29 13:12:40

标签: javascript php jquery ajax phpbb

我正在使用Ajax使用API​​将结果从php表单发布到数据库。但是当脚本运行时,我没有得到任何回报,说它是成功或错误。我可以登录数据库并看到它已添加了条目,但是当它保存到数据库时我没有收到警报。

我希望脚本能做的是:

- 首先保存到数据库(完成)

-Second:提醒用户操作已成功完成或错误

-Third:如果成功则重置表单中的值,如果出现错误则保留值

这是我迄今为止尝试过的内容:

$(document).ready(function () {
function showSuccess(message) {
    $('#success.success').append('<h3 class="alert alert-success">' + message + '</h3>').fadeIn(1000).fadeOut(5000);
}
function showError(message) {
        $('#success.success').append('<h3 class="alert alert-danger">' + message + '</h3>').fadeIn(1000).fadeOut(5000);
    }
$('form#directory-create').on('submit', function (e) {
    //stops the submit action
    e.preventDefault();
    //format the data into javascript object
    var data = $(this).serializeArray();
    //calls function to create record, passing participant information as arguments
    createRecord(data);
});
function resetStudyInfo() {
    //resets all form values to default
    $('form#directory-create').find('input:text, input:radio, input:email, input:phone').val('');
    return true;
}
function createRecord(data) {
    //converts into json data format
    var myData = JSON.stringify(data);
    console.log(myData);

    $.ajax({
        //setup option for .ajax func
        type: "POST",
        url: "directory-create-record.php",
        data: {
            //user_data : contains all the fields and their data
            user_data: myData
        },
        //shows output message on error or success
        success: function () {
            showSuccess('Study created successfully, you may now add participants to this study.');
            var reset = resetStudyInfo();
            return true;
        },
        error: function () {
            showError('Unable to create the study, did you fill out everything?');
            return false;
        }

    });
}
});

PHP方面:

 require "RestCallRequest.php";
function insertData($data_from_user){
    $status = 2;
    $url = "xxxx";
    $token = "mytokenishere";
    $fname = $data_from_user[0]->value;
    $lname = $data_from_user[1]->value;
    $title = $data_from_user[2]->value;
    $school = $data_from_user[3]->value;
    $facultystafftrainee = $data_from_user[4]->value;
    $email = $data_from_user[5]->value;
    $phone = $data_from_user[6]->value;
    $record_id = $lname .'_'. $fname;
    # an array containing all the elements that must be submitted to the API
    $data = "record_id,f_name,l_name,title,school,facultystafftrainee,email,phone,directory_complete\r\n";
    $data .= "$record_id,$fname,$lname,$title,$school,$facultystafftrainee,$email,$phone,$status";
    $args = array(
        'content'   => 'record', 
        'type'      => 'flat', 
        'format'    => 'csv', 
        'token'     => $token,
        'data'      => $data
    );
    # create a new API request object
    $request = new RestCallRequest($url, 'POST', $args);
    # initiate the API request
    $request->execute();
    $result = $request->getResponseBody();
    if($result == '1'){
        return 1;      
    }
    }

非常感谢任何帮助。谢谢

1 个答案:

答案 0 :(得分:0)

重置表单值时,您输入:email和input:phone,javascript会因为您不需要这些值而抛出语法错误,当您删除它们时,您的代码应该可以工作....这是完整的工作代码

$(document).ready(function () {
    function showSuccess(message) {
        $('#success.success').append('<h3 class="alert alert-success">' + message + '</h3>').fadeIn(1000).fadeOut(5000);
    }
    function showError(message) {
        $('#success.success').append('<h3 class="alert alert-danger">' + message + '</h3>').fadeIn(1000).fadeOut(5000);
    }
    function resetStudyInfo() {
        $('form#directory-create').find('input:text, input:radio').val('');
        return true;
    }

    $('form#directory-create').on('submit', function (e) {
        e.preventDefault();
        var data = $(this).serializeArray();
        createRecord(data);
});
function createRecord(data) {
    var myData = JSON.stringify(data);

    $.ajax({
        type: "POST",
        url: "directory-create-record.php",
        data: {
            user_data: myData
        },
        success: function () {
            showSuccess('Study created successfully, you may now add more participants to this study.');
            var reset = resetStudyInfo();
            return true;
        },
        error: function () {
            showError('Unable to create the study, did you fill out everything?');
            return false;
        }
    });
}
});