0

I have a form that is supposed to display a feedback message from the server once it is submitted in <div id="resposta"></div>

I can't use JQuery on my page because it is also using Mootools and it's really a mess to avoid the conflicts with those two (I tried all sorts of things I don't to bother you with). Therefore, I must use pure JavaScript here.

Once the form is submitted (after validation) it calls the function getResposta below:

function getXmlHttp() {
    var xmlhttp;
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp;
};


function getResposta(){
        var resposta = document.getElementById("resposta");

        var request = getXmlHttp();

        request.open("POST", "thanks.php", true);
        request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        request.onreadystatechange = function () {
            if (request.readyState != 4) return;

            if (request.status == 200) {
                resposta.innerHTML = '<p>' + request.responseText + '</p>';

            } else {
                alert("Erro: "+request.statusText);
            }
        };
    }
}

thanks.php:

<?php
echo "thank you";
?>

It seems that thanks.php isn't being called, although the form is correctly filled in and sent to the server.

I've tried typing in the absolute path to the file but it didn't work. So, what is wrong with this code?

Thanks for any help!

  • 1
    Should getResposta() function end with a `request.send(null)` to actually initiate a request? – dkasipovic Apr 03 '14 at 21:49
  • You need a request.send(). See [here][1]. [1]: http://stackoverflow.com/a/9713078/746754 – acarlon Apr 03 '14 at 21:50
  • Perhaps the form is submitting normally before your request fires. Have you disabled the form's onsubmit? – Scott Apr 03 '14 at 21:50
  • @D.Kasipovic I thought that parameter was optional, since I'm not sending any data to the server. But I tried including request.send(null) and it works! –  Apr 03 '14 at 21:51
  • Well null means that you are not sending anything, but you need to initiate the call nevertheless. Also, if you have mootools included why aren't you using it's ajax calls? – dkasipovic Apr 03 '14 at 21:54
  • Thanks a lot, Mootools is still unknown to me. I guess I will have to dig into it. Just add the answer so I can accept it. –  Apr 03 '14 at 21:55

0 Answers0