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

标签 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 post 发送的,甚至没有作为 FormData 的一部分附加到 form_data 中。

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

最佳答案

亲爱的 friend 们,

对不起。其实这只是我自己的一个错误。

我自己修复了这个问题,并在我的代码中发现了错误。

-- 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');

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

关于javascript - PHP - 为 Ajax Post 的表单数据附加新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31720986/

相关文章:

javascript - 运行时显示每个测试的状态

javascript - Google 跟踪代码管理器 (GTM) 和 Google Analytics 变量

php - 选择数据库结构中的表

javascript - 使用 jquery 解析数据-*

javascript - 从一个下拉列表中选择一个选项填充其他输入字段

javascript - iframe 上的 onload 事件在 ie 和 firefox/google chrome 中的工作方式不同

javascript - Angular Controller 未使用 OcLazyLoad 和 ngRoute 加载

javascript - 使用javascript获取电脑的时间并将其保存在数据库中

php - 使用先前在其他类中声明的对象

javascript - 触发多层元素的悬停行为