0

I'm using PHP and AJAX to make a post in a page. Below is the javascript code of the page.

function Post(posted_by, posted_to)
    {
        document.getElementById('post_textarea').disabled='disabled';
        document.getElementById('post_button').style.display='none';
        document.getElementById('loader').style.display='inline';

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

        var post=document.getElementById("post_textarea").value;
        var params="type=post&posted_by=" + posted_by + "&posted_to=" + posted_to + "&post=" + post //Parameters for post method..

        xmlhttpPost.open("POST","test.php",true);

        //Send headers; data sent as if it has been posted from form
        xmlhttpPost.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttpPost.setRequestHeader("Content-length", params.length);
        xmlhttpPost.setRequestHeader("Connection", "close");

        xmlhttpPost.onreadystatechange=function()
        {   
            //I can reach here...

            if (xmlhttpPost.readyState==4 && xmlhttpPost.status==200)
            {
                //BUT I CAN'T REACH HERE.. GETTING NO RESPONSE
                document.getElementById('posts').innerHTML = xmlhttpPost.responseText + document.getElementById('posts').innerHTML;
                document.getElementById("post_textarea").value=""; //Clear textbox
                hidePostBox();
            }
        }

        xmlhttpPost.send(params); //Send POST DATA to the server.. 
    }

In the PHP file, I've ECHOed a line of text to test if it works.

But I'm no getting that line of text as a repsonse. No response at all.

I was using this same javascript code in another website and it was working. And the same code here ain't working.

What might the glitch??

4
  • 1
    Have you checked the error console (Firebug or Chrome Dev Tools)? Commented Jun 19, 2012 at 6:06
  • 1
    it seems that you page might missing some div(s), make sure that the elements are not missing. else code is working fine as i've checked. Commented Jun 19, 2012 at 6:12
  • maybe you should check if this really DOES NOT return NULL, when you use getElementById() ? Commented Jun 19, 2012 at 7:01
  • Okay. I never used ERROR CONSOLE. And I used it today for the first time. And found the problem was it was not getting the PHP file as I had use HTACCESS URL Rewriting and it messed with it. This problem is solved and now I noticed another thing. The Error Console says: Refused to set unsafe header "Content-length" Refused to set unsafe header "Connection" Whats with them?? Though it is not messing with my expected output. Commented Jun 19, 2012 at 13:22

1 Answer 1

2

Okay. I never used ERROR CONSOLE. And I used it today for the first time. And found the problem was it was not getting the PHP file as I had use HTACCESS URL Rewriting and it messed with it

Sign up to request clarification or add additional context in comments.

Comments

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.