PHP - 为Ajax Post的表单数据附加新值

时间:2015-07-30 10:13:17

标签: javascript php jquery ajax

我有一些FormData将使用Ajax Post发送到另一个PHP文件。

var formElement = document.getElementById("form-id");
var form_data = new FormData(formElement);

var csrf_arr = csrf.split("=");
form_data.append(csrf_arr[0],csrf_arr[1]);

$.ajax({
    type: "POST",
    url: "your-url",
    data: form_data,
    processData: false,
    contentType: false,
    dataType: "json",
    success: function(data) {
        alert(data.message);
    },
    complete: function(data) {
        location.reload();
    }
})

我在这里要做的是在 form_data

中添加新的单个数据

根据我的假设,我可以通过这样做来实现它

form_data.append("key","value");

但它不是通过ajax发送的,甚至不是作为FormData的一部分附加到form_data。

我在这里犯了什么错误,甚至有一定的规则吗? 请提供任何帮助。 提前谢谢!

1 个答案:

答案 0 :(得分:0)

亲爱的朋友,

对不起实际上这只是我自己的错误。

我自己解决了这个问题,并在我的代码中找到了错误。

- javascript代码 -

$( '#submitYours' ).on('click', function(){

    var formElement = document.getElementById("form-id");

    // Value of formElement consist of pair of data like:
    // qst_additional_name_for_the_key=value_of_the_key

    var form_data = new FormData(formElement);

    form_data.append('break','break');

    // to prevent confusion, i'm putting the csrf variable value here
    // but i'm not using this as printed variable later, just for security check
    var csrf = "your_csrf_token_name=bb6ff34c26ff938822dd339c1682bbcc";

    var csrf_arr = csrf.split("=");

    var x = 0;
    for(x = 0; x < elementId.length; x++){
        form_data.append(elementId[x], elementVal[x]);  

        // Appended data above is just an additional data from some array of data
        // and the format is the same with formElement
        // qst_additional_name_for_the_key=value_of_the_key
    }

    form_data.append("master_id",master_id);
    form_data.append(csrf_arr[0],csrf_arr[1]);

    addLoader("overlay-loader");
    runInAnimate("overlay-loader", "bounceInUp");

    $.ajax({
        type: "POST",
        url: "url.php",
        data: form_data,
        processData: false,
        contentType: false,
        dataType: "json",
        success: function(data) {
            alert(data.message);
        },
        complete: function(data) {
            runOutAnimate("overlay-loader", "overlay-loader", "bounceOutDown");
            location.reload();
        }
    })
});

- url.php代码 -

$temp = '';
$arr_in = '';
foreach($_POST as $key => $value){
    if(!@$value) continue;

    if(substr($key, 0, 4) == 'qst_'){  // This is the real problem. 
                                       // The only data taken from the form_data
                                       // are the data containing string "qst_" in it
        if(is_array(@$value)){
            foreach(@$value as $val){
                $this->yourmodel->saveAnswer(substr($key, 4), strtolower(trim(@$val)));
                $temp = @$_POST[$key];
                $arr_in .= @$temp;
            }
        }else{
             $this->yourmodel->saveAnswer(substr($key, 4), strtolower(trim(@$value)));
             $temp = @$_POST[$key];
             $arr_in .= @$temp;
        }
    }else if($key == 'break'){    // remember my additional data? The string "break"
                                  // i just have to chek it here and put into my $arr_in variable,
                                  // so that I can use it now, because the previous conditional allow
                                  // only data containing string "qst_"
         $temp = @$_POST[$key];
         $arr_in .= @$temp;
    }
}

print json_encode(array('status' => 'success', 'message' => @$arr_in));

结论是,如果我们只是手动附加form_data

,没有错
form_data.append('your_key','your_value');

一切都与您在发送到另一个文件后如何访问数据有关。 我的错误,大家好! 对不起,非常感谢你的帮助。 :)