0
var workerEdit = { 
    id: id, 
    name: $('#workers_response input[name="name"][data-id="gear-'+id+'"]').val(),
    photo: $('#workers_response input[name="pas-scan"][data-id="gear-'+id+'"]')[0].files[0],
    bibliography: $('#workers_response textarea[name="bibliography"][data-id="gear-'+id+'"]').val(),
    history: $('#workers_response textarea[name="history"][data-id="gear-'+id+'"]').val(), 
    salary: $('#workers_response input[name="salary"][data-id="gear-'+id+'"]').val()
}

i create this object, then i use ajax to send data throw method POST:

$.ajax({
    type: "POST",
    url: "/admin-workers/edit_worker.php",
    data: 'edit_worker='+JSON.stringify(input_data),

    success: function(data) { 
        var obj = JSON.parse(data); 
        if(obj.error === undefined) { 
            console.log(obj); 
            if(obj.data === null) { 
                alert("Успех"); 
            } 
        } 
        else { 
            alert("Ошибка: "+obj.error); 
            console.log(obj); 
        } 
    }, 
});

and in php i use:

$edit_worker = json_decode($_POST['edit_worker']);
$test = $edit_worker->name;

Why doesn't php get the object?

10
  • In your browser's debugging tools, what are the actual details of the AJAX request? Is it a POST request? What does the data look like? In the server-side code, if $_POST['edit_worker'] doesn't contain valid JSON, does it contain anything at all? What are its raw contents? Commented Sep 22, 2017 at 15:55
  • Show the full code, those two ajax lines aren't enough Commented Sep 22, 2017 at 15:55
  • It's possible data should be a key-value object, not a string. Look at the request. Commented Sep 22, 2017 at 15:56
  • Check the network tab in dev tools and see the status of the request... Commented Sep 22, 2017 at 15:56
  • added more information Commented Sep 22, 2017 at 15:59

2 Answers 2

1

Your data is not properly serialized. You cannot just stringify a JSON map and POST it as 'edit_worker='+JSON.stringify(input_data) It should look like this:

key1=value1&key2=value2&key3=value3

On the backend, you will extract individual values by their keys:

If ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $val1 = ( isset($_POST['val1']) ) ? trim($_POST['val1']) : '';
    $val2 = ( isset($_POST['val2']) ) ? trim($_POST['val2']) : '';
    // etc. etc. 

}

Your validation probably needs to contain more than just trim, depending upon what the data source is and what you're doing with it, but that's another discussion.

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

Comments

0

You are creating an object with variable workerEdit but your are posting the data from input_data. It appears you are not posting any data because of incorrect variable names.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.