Difference between filter_input and direct acces on $_POST after objective ajax request

纵饮孤独 提交于 2019-12-11 06:18:37

问题


I have different results by using filter_input(INPUT_POST, 'attribute') and $_POST['attribute'] and don't know why this happens.

The Post-Request is send by a JavaScript build with JQuery and looks like that:

// type javaScript
var formData = {
  field_a: "valueA",
  field_b: "",
  field_c: undefined
};
$.ajax({
  url: 'serverAddress',
  data: {action: 99, formData: formData},
  dataType: 'json',
  method: 'post',
  success: function(){
    console.log(arguments)
  }
});

My PHP-Script looks like that:

// type php
$requestMethod = INPUT_POST;
$response = [
  "fi-result" => filter_input($requestMethod, 'formData'),
  "direct-result" => $_POST['formData'];
];
echo json_encode($response);

the result what is coming back is not what i was awaiting because the access via filter_input returns falsein my tests and not an json object like the direct access on the super global $_POST.

// type json response
{
  "fi_result": false,
  "direct-result": {
    "field_a": "valueA",
    "field_b": ""
  }
}

Why are there differences between using filter_input and direct access on $_POST?

I don't want to access the super global $_POST. Is there any way to use filter_input like above without encode formData to a String in JavaScript and decode it in PHP one simple step after encoding?

By the way. I'm using TypeScript to generate my JavaScript. That is not supporting the FormData Object (transpiler throws error on new FormData()). So i can't use this.


回答1:


I found the answer deep in the PHP docs. POST is not build to transport deep object. And filter_input method tries to get simple datatypes like string or int. this method does not parse internal so i have to send it as JSON string and decode it or i can't use filter_input in my case.

i took the first and send now strings.



来源:https://stackoverflow.com/questions/41717877/difference-between-filter-input-and-direct-acces-on-post-after-objective-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!