Home » Javascript » Unable to save data from javascript to MySql using PHP

Unable to save data from javascript to MySql using PHP

Posted by: admin August 19, 2018 Leave a comment

Questions:

I have a problem that I unable to save data that are parsed from Javascript into PHP. The problem came when I implement WordPress. On single index.html it is working and data able to store in the database.

On Javascript file : (send data)

        var dbParam = JSON.stringify(data);
        console.log(dbParam);
        var obj, dbParam, xxmlhttp;
        xxmlhttp = new XMLHttpRequest();
        xxmlhttp.onreadystatechange = function () {
            if (this.readyState == 4 && this.status == 200) {
                // document.getElementById("demo").innerHTML = this.responseText;
            }
        };
        xxmlhttp.open("GET", "http://localhost/trackpage/dummy-data/saveDB.php?x=" + dbParam, true);
        xxmlhttp.send();
        console.log("send");

On PHP file: (get data)

header("Content-Type: application/json; charset=UTF-8");
$obj = json_decode($_GET["x"], false);
$conn = new mysqli ("localhost", "root", "", "laravelcrudwebapp");
$stmt = $conn->prepare("INSERT INTO tracks (data1,data2) VALUES (?, ?, )");
$stmt->bind_param("ss",$obj->data1,$obj->data2);
$stmt->execute();
$stmt->close();
$conn->close();
Answers: