1

I'm trying to send a JavaScript variable to be processed server-side by PHP then echo back the result. My ajax request results in readyState == 4 and status == 200 yet PHP keeps echoing back an empty array/value.

I'm pretty sure this comes from my Ajax call sending an empty request but everything I tried didn't work, so maybe I'm going all wrong here.

JS (from index.html) :

var valueToSend = Math.random(); 

var request = false;
if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
    request = new ActiveXObject('Microsoft.XMLHTTP');
}

function send_ajax() {
    if (request) {
        request.open('POST', 'test.php', true);
        request.onreadystatechange = function() {
            if (request.readyState == 4 && request.status == 200) {
                console.log(request.responseText);
            }
        }
        request.send(valueToSend); //this line is probably not sending the value the way I expect it to
    }
}

PHP (from test.php) :

echo $_POST['name']; //simple echo to check if PHP indeed receives valueToSend

My last try was to change request.send(valueToSend); with .send(encodeURI('name=' + valueToSend)); but it just made the ajax call redirect the page location to a non-existing one.

What am I doing wrong ?

2
  • Is the path to test.php correct wrt the file that this is executing in? Commented Apr 2, 2017 at 18:16
  • Just double checked : yes it is correct. Commented Apr 2, 2017 at 18:20

1 Answer 1

2

There are few things wrong in your code, such as:

  • Even through you defined send_ajax() function, you didn't call it anywhere in the code.
  • With POST request you need to send an HTTP header setRequestHeader() along with the request, like this:

    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    
  • echo $_POST['name']; in test.php page won't work because name is undefined here. Your send() method call should be like this:

    request.send("name="+valueToSend);
    

So the complete JavaScript code would be like this:

var valueToSend = Math.random(); 

var request = false;
if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
    request = new ActiveXObject('Microsoft.XMLHTTP');
}

function send_ajax() {
    if (request) {
        request.open('POST', 'test.php', true);
        request.onreadystatechange = function() {
            if (request.readyState == 4 && request.status == 200) {
                console.log(request.responseText);
            }
        }
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        request.send("name="+valueToSend);
    }
}

send_ajax();
Sign up to request clarification or add additional context in comments.

1 Comment

Oh my god... I totally forgot about the setRequestHeader() method... Works perfeclty fine now. 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.