I have converted all the entries of a form into an object and then fill it using the object to fill an array:
var jsonObj = [];
var formData = new FormData();
$('[name="'+form+'"] *').not(':input[type=file], :input[type=button], :input[type=submit], :input[type=reset], [name="input_search"]').filter(':input').each(function(obj, v) {
var iObj = {}
var input = $(this);
var inputname = input.attr("name");
var val = input.val();
var inputval = val;
iObj[inputname] = inputval;
jsonObj.push(iObj);
});
Later i try to convert the Final array into a Json String with this:
jsonData = JSON.stringify(jsonObj);
and i get this String:
[{"s-file[]":"Prueba 3"},{"text_file[]":"ORT0000133.pdf"},{"idform":"f-gen-desk"},{"idprocess":"p-save"}]
i send this to PHP Server With this, becouse i send other Element type File with formData.append method:
formData.append('jsonData', jsonData);
var url = 'index.php';
$.ajax({
url: url,
data: formdata,
contentType: false,
processData: false,
type: 'POST',
cache: false,
error: function(xhr, status, error) {
alert(error);
},
success: function(response) {
alert(response);
}
});
In PHP server Side i try to get this string:
echo var_dump($_POST['jsonData']);
and i get this string:
string(169) "[{"s-file[]":"Prueba 3"},{"text_file[]":"ORT0000133.pdf"},{"idform":"f-gen-desk"},{"idprocess":"p-save"}]"
when i try to conver this to and Array with this:
Test 1:
echo '<pre>';
echo var_dump($_POST);
echo '</pre>';
ouput:
array(1) {
["jsonData"]=>
string(169) "[{"s-file[]":"Prueba 3"},{"text_file[]":"ORT0000133.pdf"},{"idform":"f-gen-desk"},{"idprocess":"p-save"}]"
}
Test 2:
echo '<pre>';
$jsonData=$_POST['jsonData'];
$Data=json_decode($jsonData);
echo var_dump($Data);
echo '</pre>';
ouput: I get NULL.
I do not understand what I do wrong
Last Update:
i run this test:
echo '<pre>';
$jsonData = '|'.$_POST['jsonData'].'|';
$json = '|'.'[{"s-file[]":"Prueba 3"},{"text_file[]":"casa.jpg"},{"idform":"f-gen-desk"},{"idprocess":"p-save"}]'.'|';
echo $Line = mb_strlen(mb_strcut($jsonData, 0, strspn($jsonData ^ $json, "\0")));
echo '<br>';
echo $jsonData[$Line];
echo '<br>';
echo $json[$Line];
echo '<br>';
echo '</pre>';
Output
3
&
"
I can determine that the chain is corrupt, but I can not know why or how or how to fix it.