I am trying to send data from user end to server end: positionList.html <=> dbUpdate.php
issue seen on the developer console
The data format I am going to send is an array of self-defined Javascript objects. Here's one example: [{"position":"code testing","description":"junior"},{"position":"front end developing","description":"junior"}]. There are two objects in the array above, and each one is a self-defined JS object. The object definition is stated below, it has two properties:
function Entry(inpt1, inpt2) {
this.position = inpt1;
this.description = inpt2;
}
let myJson = JSON.stringify(msgPack);
let url = "dbUpdate.php";
$.post(url, myJson, function(data){
console.log("getting data back...");
console.log('type of data is: ' + typeof(data));
console.log("data: " + data);
alert(JSON.parse(data));
});
<?php
require_once 'pdo.php';
$data = isset($_POST)? $_POST:'nothing here';
foreach($data as $obj) {
echo( json_encode($obj['position']));
}
/*
foreach($data as $obj) {
foreach($obj as $k => $v) {
echo "json_encode({$k} => {$v})";
}
}
*/
?>
My questions are:
- Any suggestions regarding the AJAX communication? Is the problem happening at the back-end side? It couldn't get any information from $_POST.
- How does PHP back-end know there's a message coming and what methods do we developer have to check that Other than isset($_POST)? My concern is the front-end could send multiple data with different contents.
Thanks.