I'm trying to send a body as a query to a PHP script, but when I treat the information as "$query['param']", this information is returned as undefined.
The JavaScript code I'm using to send the information is as follows:
function insertNewRecord(data) {
fetch('/configs/database/add.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `name=${data.name}&contact=${data.contact}&delivery=${data.delivery}&value=${data.value}`
})
.then(function(response) {
if (response.ok) {
return response.text();
}
throw new Error('Error in the request.');
})
var table = document.getElementById("employeeList").getElementsByTagName('tbody')[0];
var newRow = table.insertRow(table.length);
cell1 = newRow.insertCell(0);
cell1.innerHTML = data.name;
cell2 = newRow.insertCell(1);
cell2.innerHTML = data.contact;
cell3 = newRow.insertCell(2);
cell3.innerHTML = data.delivery;
cell4 = newRow.insertCell(3);
cell4.innerHTML = data.value;
cell4 = newRow.insertCell(4);
cell4.innerHTML = `<a onClick="onEdit(this)">Edit</a>
<a onClick="onDelete(this)">Delete</a>`;
}
And the PHP script I'm using to receive the request is as follows:
<?php
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$CurPageURL = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$parts = parse_url($CurPageURL);
if(count($parts) <= 3) return;
parse_str($parts['query'], $query);
$conn = OpenCon();
$sql = "INSERT INTO users (name, contact, delivery, value) VALUES (" . $query['name'] . ", " . $query['contact'] . ", " . $query['delivery'] . ", " . $query['value'] . ")";
$result = $conn->query($sql);
?>
What should I do/change to fix this problem? I'm trying to do a CRUD in PHP, but this problem has been disturbing my work and I'm not able to solve it myself.