javascript - PHP - Append New Value to Form Data for Ajax Post -


i have formdata going sent php file using ajax post.

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 here append new single data inside form_data

from assumption, make doing this

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

but not sent through ajax post or not appended form_data part of formdata.

am making mistake here or there rule that? please give help. in advance!

dear of friend,

i'm sorry. mistake myself.

i have fix myself , found mistake right inside code.

-- javascript code --

$( '#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');      // prevent confusion, i'm putting csrf variable value here     // i'm not using printed variable later, 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 additional data array of data         // , format same 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 code --

$temp = ''; $arr_in = ''; foreach($_post $key => $value){     if(!@$value) continue;      if(substr($key, 0, 4) == 'qst_'){  // real problem.                                         // data taken form_data                                        // data containing string "qst_" in         if(is_array(@$value)){             foreach(@$value $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 additional data? string "break"                                   // have chek here , put $arr_in variable,                                   // can use now, because previous conditional allow                                   // 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 append form_data manually

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

everything matter of how access data after sent file. mistake, everyone! i'm sorry , lot help. :)


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -