Mlagma
Mlagma

Reputation: 1260

Post request through ajax doesn't appear to be working properly

I have form that makes a request via post managed by ajax. However, it does not appear to be working at all. I know the php file works as I've tried it without using ajax in the middle.

This is my ajax request:

function checkOut(params) {
    var urlString = params;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    } else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("test").innerHTML=xmlhttp.responseText;
        }
       //Setup for post submission  
       xmlhttp.open("POST","mockcheckout.php",true);
       xmlhttp.setRequestHeader("Content-length", params.length);
       xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

       xmlhttp.send(urlString); 

    }

} 

Any help is appreciated.

Upvotes: 0

Views: 271

Answers (1)

Viktor S.
Viktor S.

Reputation: 12815

Looks like you have a problem with code blocks. .open, .send should be outside of onreadystatechange handler scope.

function checkOut(params) {
    var urlString = params;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    } else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("test").innerHTML=xmlhttp.responseText;
        }
    }
    //Setup for post submission  
    xmlhttp.open("POST","mockcheckout.php",true);  
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");  
    xmlhttp.send(urlString);     
} 

Upvotes: 2

Related Questions