0

Well I followed this question, but cannot send array to php it returns me always empty:

JS:

            values = [];
            values['mpsRegnomer'] = $('#mpsRegnomer').val();
            values['mpsMarka'] = $('#mpsMarka').val();
            values['mpsMarkaOther'] = $('#mpsMarkaOther').val();
            values['engineType'] = $('#engineType').val();
            values['seatNumberInput'] = $('#seatNumberInput').val();
            values['carColor'] = $('#carColor').val();
            values['mpsChassiNum'] = $('#mpsChassiNum').val();
            values['mpsModel'] = $('#mpsModel').val();
            values['mpsModelOther'] = $('#mpsModelOther').val();
            values['mpsManufactureDate'] = $('#mpsManufactureDate').val();
            values['mpsfor'] = $('#mpsfor').val();
            values['VehicleType'] = $('#VehicleType').val();
            values['dvigatelInput'] = $('#dvigatelInput').val();
            values['engineMaxPower'] = $('#engineMaxPower').val();
            values['is_automatic'] = $('#is_automatic').val();
            console.log(values);

            $.ajax({
                    type: 'POST',
                    url: 'assets/clients/services/saveDataMPS.php',
                    async: false,
                    dataType: "JSON",
                    data: {"values": JSON.stringify(values)},
                    success:function(response){
                        alert(1);
                    }
            });

console.log(values) show me that array is OK.

PHP code:

<?php

var_dump(json_decode($_POST["values"])); exit; 

It returns me always empty, also tried only with var_dump($_POST); same result.. Where am I wrong?

Result from console.log(values):

enter image description here

3
  • 2
    You don't need to JSON.stringify(values) instead, data: { values: values } Commented Jan 29, 2019 at 13:14
  • can you add JSON.stringify value into the question Commented Jan 29, 2019 at 13:14
  • @Cid with data: {"values": values} and data: {values: values} and var_dump($_POST) still empty Commented Jan 29, 2019 at 13:18

1 Answer 1

5

You defined values as an array and therefore when you stringify it, you get an empty array. Define it as an object like values = {}; and it will work.

You don't use associative arrays in javascript because

If you use named indexes, JavaScript will redefine the array to a standard object. After that, some array methods and properties will produce incorrect results.

Thats's why you need to define it as object in the beginning.

values = {};
values['mpsRegnomer'] = $('#mpsRegnomer').val();
values['mpsMarka'] = $('#mpsMarka').val();
values['mpsMarkaOther'] = $('#mpsMarkaOther').val();

Here is a working JSFiddle. http://jsfiddle.net/pk97fe0b/

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

3 Comments

You are right it works with object, but how do I make ti to work with array ?
Why do you need it to be an array and not an object? Associative arrays in javascript are objects and you use objects for that purpose. If you use named indexes, JavaScript will redefine the array to a standard object. After that, some array methods and properties will produce incorrect results.
I don't need it, i'm just curious :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.