简体   繁体   English

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

[英]PHP - Append New Value to Form Data for Ajax Post

I have some FormData that are going to be sent into another PHP file using Ajax Post. 我有一些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();
    }
})

What I'm trying to do here is to append a new single data inside the form_data 我在这里尝试做的是在form_data内添加一个新的单个数据

From my assumption, I could make it just by doing this 根据我的假设,我可以做到这一点

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

But it is not sent through the ajax post or even not appended to the form_data as part of the FormData. 但是它不会通过ajax发布发送,甚至不会作为FormData的一部分附加到form_data中。

Am I making any mistake here or even there is certain rule for that? 我在这里犯任何错误,甚至有一定的规则吗? Please give any help. 请给予任何帮助。 Thanks in advance! 提前致谢!

Dear all of my friend, 亲爱的我所有的朋友,

I'm sorry. 对不起。 Actually it is just a mistake from myself. 实际上,这只是我自己的一个错误。

I have fix this myself and found the mistake right inside my code. 我自己修复了此错误,并在我的代码中发现了错误。

-- The javascript code -- -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();
        }
    })
});

-- The url.php code -- -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));

The conclusion is, nothing wrong if we just append the form_data manually 结论是,如果我们只是手动附加form_data,那没有错

form_data.append('your_key','your_value');

Everything just the matter of how you access the data after it sent to another file. 一切都与将数据发送到另一个文件后如何访问数据有关。 My Mistake, everyone! 我的错,大家! I'm sorry and thanks a lot for your help. 很抱歉,非常感谢您的帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM