I've written a XMLHtttpRequest, but something is breaking down somewhere.
PHP Code:
function submit(){
// declaring variables to be used
var xhr, target, changeListener, url, data;
//setting url to the php code to add comments to the db
url = "createCampaign.php";
data = document.getElementById("source").innerHTML;
console.log("Sending", data);
// create a request object
xhr = new XMLHttpRequest();
changeListener = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
console.log("Response", this.responseText);
// result = JSON.parse(this.responseText);
//injectContent(result.id, form);
}
}
};
// initialise a request, specifying the HTTP method
// to be used and the URL to be connected to.
xhr.onreadystatechange = changeListener;
xhr.open('POST', url, true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(data);
return false;
}
I've tried changing the data variable so that it contains just a simple string but it still does not work.
In the PHP script i'm trying to see if anything has been received by using this line 'echo $_REQUEST['data'];' but it appears that nothing has been sent.
Any help will be greatly appreciated.
Thank you.