0

Problem: The PHP $_POST[] cannot get any key or elements.

What is an appropriate data structure for this case?

//Data format:

[{"ENG_NAME_OF_OWNER":"xxx xxx xxx",
"CAPACITY":"", 
"MEMORIAL_NUMBER":"08060500610190ASSIGNMENT WITH PLAN",
"DATE_OF_INSTRUMENT":"19/05/2008",
"DATE_OF_REGISTRATION":"05/06/2008",
"CONSIDERATION":"$2,668,000.00",
"REMARKS":"",
"CHI_NAME_OF_OWNER":"xxx xxx xxx"},

 {"ENG_NAME_.........":""},

 {............}]


var array = //Data above;
var URL = "php/landSearchOwnertoDB.php";
var Data = JSON.stringify(array);

$.ajax({url: URL, data: Data, type: "POST",contentType: 'application/json',
        success: function(modelData) {
        alert(modelData);
   }
});

Should I get rid of Array when posting data to php $_POST[]?

Spokey
  • 10,974
  • 2
  • 28
  • 44
Paco_BB
  • 9
  • 1

1 Answers1

1

No need to stringify my friend. Just pass it as it is:

And just remove contentType: 'application/json'. Just send it as a normal POST

$.ajax({
    url: URL,
    data: {data: array}, // send the normal array
    type: 'POST',
    dataType: 'JSON',
    success: function(modelData) {
        console.log(modelData);
    }
});

Then in the server, access the data:

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $data = $_POST['data']; // holds your values
    // it holds data, most likely you will need to loop this
}
Kevin
  • 41,694
  • 12
  • 53
  • 70