Despite searching extensively, I couldn't find the solution to this particular issue on Stack Overflow.
Currently, my focus is on saving a large amount of HTML data to a database using AJAX JavaScript with PHP. Below is the snippet of my JavaScript code:
function save()
{
var hist = document.getElementById("hist").value;
var mission = document.getElementById("mission").value;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200 )
{
UserAccountInfo = this.responseText;
alert(UserAccountInfo);
}
}
xmlhttp.open("POST","savecompany.php",true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("history="+hist+"&mission="mission);
}
The issue arises at this line of code:
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
If I were to comment out that line, I receive the plain string in the alert and the database successfully stores the plain string.
Below is the content of my PHP file:
<?php
require "conn.php";
$history= $_POST["history"];
$mission = $_POST["mission"];
$sql = " UPDATE company SET history ='$history' , mission='$mission' where id='1'";
mysqli_query($conn,$sql);
echo $history;
mysqli_close($conn);
?>
I'm struggling to pinpoint the error in my implementation. Any insights would be greatly appreciated.