1

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();
2
  • What is the output when you var_dump($obj); exit; ? Did you check it? Commented May 23, 2018 at 8:50
  • how to check the var_dump($obj) i try using echo to send data to test. Because i use wordpress Commented May 23, 2018 at 9:03

1 Answer 1

1

If you're going to send your data as a GET parameter, you have to URL-encode it first:

var dbParam = encodeURIComponent(JSON.stringify(data));

However, I will point out that it is extremely bad practice to perform write operations via GET requests. You're leaving yourself wide open for all sorts of abuse and cross-side scripting attacks. For starters, you can read this post, as well as do some research on Google:

https://softwareengineering.stackexchange.com/questions/188860/why-shouldnt-a-get-request-change-data-on-the-server

Sign up to request clarification or add additional context in comments.

1 Comment

this is the answer for the question. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.