0

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.

12
  • what?? sorry, it's really hard to understand 1st what you wanna do 2nd what the problem is. Commented May 30, 2018 at 23:06
  • @Jeff Updated and Corrected my question. Commented May 30, 2018 at 23:19
  • 1. Look at and/or post the JSON so we can too. 2. RTFM and php.net/manual/en/function.json-last-error-msg.php Commented May 30, 2018 at 23:26
  • i have added this string Commented May 30, 2018 at 23:26
  • WFM: 3v4l.org/4e6G4 Commented May 30, 2018 at 23:28

2 Answers 2

0

Simpler way to send json is:

$.ajax({
    url: url,
    data: jsonData, // stringified JSON
    contentType: 'application/json',
   // processData: false,
    type: 'POST',
    //cache: false, POST won't cache
    error: function(xhr, status, error) {
        alert(error);
    },
    success: function(response) {
        alert(response);
    }
});

And receive in php

$data = json_decode(file_get_contents('php://input')[,boolean to force array]);

Easiest way to send data in general is to use serialize() on form and send as default content type and receive various fields as $_POST[inputName]

Sign up to request clarification or add additional context in comments.

Comments

0

The problem may be related to the use of:

$_GET  = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

https://stackoverflow.com/a/9854121/963200

need work around it to solve and use after read the data from POST and GET

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.