Home>

(I do everything locally via OpenServer) I send data from JS to PHP but for some reason $ _post returns an empty array and I can not work with it. In this case, the request is processed and returns to 0 in Console.log. When you write echo ($ _ post) or echo ($ DATA) it returns Array

js


$ (Document) .on ('Click', '.btn-Save', Function () {
    Let Btn= $ (this),
        edit_btn= btn.siblings ('. BTN-EDIT'),
        BTNS= BTN.Parents ('. Btns'),
        id= btn.parents ('. Table-Item'). Data ('ID'),
        text= btns.siblings ('. Message'),
        text_msg= text.val ()
    $ .post ('../../../php /functions.php', {ID, text_msg}, Function (DATA) {
        $ (BTN) .attr ('Disabled', True)
        $ (text) .attr ('disabled', true)
        $ (edit_btn) .RemoveAttr ('disabled')
        Console.log (DATA);
    })
})

PHP

<
? php.
$ Data= $ _Post ['Data'];
$ id= (int) $ data ['id'];
$ MSG= $ Data ['Text_msg'];
$ json= file_get_contents ('../users.json');
$ users= json_decode ($ JSON, TRUE);
Foreach ($ Users AS $ Param) {
    echo ($ ID. '' $ param ['id']);
    if ($ param ['id']== $ ID) {
        $ param ['message']= $ msg;
        $ contents= json_encode ($ users);
        file_put_contents ('../users.json', $ contents);
        echo ('success');
    } ELSE {
        echo ('error');
    }
}
? ≫

VAR_DUMP ($ _ POST)

andreymal2021-08-29 16:38:05

Maybe I'm mistaken, but did you install the handler on the post request?

Dmitriy Nikoalev2021-08-29 16:57:05

You form Data as {ID, text_msg} (i.e., as a list of values. And you need {ID: ID, text_msg: text_msg} (parameter: value)

Евгений Малеев2021-08-29 17:34:58

PHP does not translate into $ _post data if they are not Form-Data, you have JSON

ArchDemon2021-08-29 17:44:11