So I am capturing data from an API and storing it in an array that looks like this: stackVar[{user, red, 12}, {user1, green, 13}]
Then I am trying to send that with _POST. After testing I realized I am in fact saving the array properly because console.line(stackVar) works. I also noticed that I am opening the connection to the php file but it closes the connection before posting any data to the php page. Here is what my front end looks like.
if(results.length == stackVar.length){
var request = new XMLHttpRequest();
request.open('POST', 'http://www.server.com/saveF.php', true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.send(JSON.stringify(stackVar));
}
and my back end.
<?php $conn = new mysqli($sn, $un, $pw, $db);
if ($conn->connect_error) {
die("connection failed: " . $conn->connect_error);
}
$bInfo = $_POST["stackVar"];
$infoEncoded = json_encode($bInfo);
$getsome = $infoEncoded[0][0];
$sql = "INSERT INTO companies(company) VALUES ('$getsome')";
echo $getsome;
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close()
?>