0

I am in need of a little help with a small bit of my code that is essential to my application. I am making a small clicker game, and I want users to be able to save and load data via PHP to my server. I do not want to use Local Storage to make it harder for anyone to edit their economy and "cheat". When the user clicks on a save button I have, it fires my vue method which initializes the saving. I have had no problem getting the data into a JSON format, however I cannot get PHP to read this data via POST. I have checked for network headers, and it shows that stuff is being sent, it seems that PHP just isn't catching it. I'll include the code for the JS part and PHP part below. The PHP is only set to echo if the array_key_exists right now, as after getting this sorted out I will easily be able to handle the rest. Any help would be greatly appreciated!

I have tried to follow this, which has not worked so far Send JSON data from Javascript to PHP?

JS

saloOut: function() {
            var saveData = {
                saveMoney: this.money,
                saveCrystals: this.crystals,
            };
            saveData = "saveData=" + (JSON.stringify(saveData));
            var sendData = new XMLHttpRequest();
            sendData.open("POST", "salo.php", true);
            sendData.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            sendData.send(saveData);
            console.log(saveData);
        }

PHP

<?php
    if (array_key_exists("saveData", $_POST)) {
        echo "<p>SALO Ready!</p>";
    }
?>
Elderis
  • 17
  • 9
  • You see the request in inspector Network tab? If yes click on it, check "Request Method" if it is POST and then go to Request Payload and see if the params are there – Răducanu Ionuţ Apr 14 '19 at 18:13
  • Please share the server logs if you observe any errors there. – Nagama Inamdar Apr 14 '19 at 18:13
  • @RăducanuIonuţ I checked and the Request Method is POST, however I do not see a Request Payload. It has a Form Data area, which does show the content that I am sending however – Elderis Apr 14 '19 at 18:41
  • @Naina Sever logs are clean as can be (No Errors) – Elderis Apr 14 '19 at 18:43

1 Answers1

0

Decode the JSON string at the PHP end before accessing values, like this:

<?php
    if(isset($_POST['saveData'])){
        $result = json_decode($_POST['saveData'], true);
        // use $result['saveMoney'] and $result['saveCrystals'] accordingly
    }
?>

Update# 1:

As OP commented below, I expect that it will print "SALO Ready" but it is instead doing nothing

That's because you are not using responseText property of XMLHttpRequest object to see the text received from the server. Use below snippet to see the response text.

saloOut: function() {
    var saveData = {
        saveMoney: this.money,
        saveCrystals: this.crystals,
    };
    saveData = "saveData=" + (JSON.stringify(saveData));
    var sendData = new XMLHttpRequest();
    sendData.open("POST", "salo.php", true);
    sendData.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    sendData.send(saveData);

    sendData.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            alert(this.responseText);
        }
    };
}

Reference: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseText

Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • I haven't tried to access the values yet, right now I was just checking for if the POST data exists, same way as if a normal form is submitted. My problem is from how my PHP is acting, the data doesn't exist at all – Elderis Apr 14 '19 at 18:47
  • @Elderis What do you mean by *My problem is from how my PHP is acting, the data doesn't exist at all*? What exactly is not working for you? Please state your *expected* vs. *actual* output. – Rajdeep Paul Apr 14 '19 at 18:50
  • On my network tab in Chrome, it shows the data being sent to the PHP file via POST and it shows that it has the data. But when I test for the POST data in my PHP, nothing happens.I am testing for it the same way I would test for POST data from a form – Elderis Apr 14 '19 at 18:55
  • I expect that it will print "SALO Ready!" but it is instead doing nothing – Elderis Apr 14 '19 at 18:58
  • @Elderis I have updated my answer, please see the **Update# 1:** section of my answer. – Rajdeep Paul Apr 14 '19 at 19:06
  • Thank you for the answer! Next I will have to figure out getting the data into one column of the database, however the question here is answered. – Elderis Apr 14 '19 at 19:48