javascript code (xmlHTTPRequest):

function initAll(lstrgData){

loadDoc(lstrgData);

}

function loadDoc(theList){

        var json = '{"team": "'+theList+'"}';
        var txt = JSON.parse(json);
        txt = JSON.stringify(txt);
        var vars = "jstring="+txt;
        alert("json = "+json);
        var xmlhttp = new XMLHttpRequest();

        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState === 4) {
                if (xmlhttp.status === 200) {
                    alert("you are now here");
                }
                else if (xmlhttp.status === 400) {
                    alert('There was an error 400');
                }
                else {
                    alert('something else other than 200 was returned');
                }
            }
        };
        xmlhttp.open("POST", "myJSON.php", true);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.send(vars);

}

PHP Page accessed from xmlHTTPRequest .send(vars):

<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
header("Content-Type", "application/x-www-form-urlencoded");
$fileName = "myJSON.json";
$var1 = file_get_contents($fileName);
if(isset($POST["jstring"])){
$var2 = $
POST["jstring"];
file_put_contents($fileName, $var2);
}
else{
echo "There was a problem getting the data";
}
?>
</body>
</html>

My hosting server (the server upon which the above code resides), and which I do not own, and am paying for the service, has issues with the javascript code above, always going to the "else { alert('something else other than 200 was returned');" part of the if statement. I have no idea why this is happening, as this code runs fine on my local machine (apache server). Any help would be appreciated on this issue.

Also, both servers are having an issue with the php page, and in particular, it seems that the 'jstring' index is undefined, and I have no idea why, as this is the information passed to the $_POST['jstring'] superglobal sent by the xmlhttp.send(vars) statement in the javascript code; at least, that is the goal. Something strange going on with the local machine, in that the data from the xmlHTTPRequest is still getting through, and the json file is being updated, even though the "else" portion of the if statement is always executed on the php if statement. Any help would be grea
}

    jetjock99;11064831 wrote:
                            alert('something else other than 200 was returned');
                     

    Hello, and welcome to PHPBuilder. 🙂

    I suppose the first thing to do might be to determine what the error actually WAS:

    alert('something else other than 200 was returned, and it was: '+xmlhttp.status);

    (Something here about console.log instead of window.alert, this being a PHP forum instead of a JavaScript one, using CODE and PHP tags, etc. etc. blah blah ...)

      Write a Reply...