Ajax: parameter passing not working on IE

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jchoi1009
Forum Newbie
Posts: 1
Joined: Thu Jun 04, 2009 11:06 am

Ajax: parameter passing not working on IE

Post by jchoi1009 »

I have this simple code to test something:

Code: Select all

<?php
if (isset($_POST['c'])){
    echo $_POST["c"];
    return;
}
?>
 
<html>
    <head>
        <script type="text/javascript">
        <!--
            function ajax(){
                var activexmodes=["Microsoft.XMLHTTP","Msxml2.XMLHTTP"]; //activeX versions to check for in IE
                if (navigator.appName == "Microsoft Internet Explorer" && window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
                    for (var i=0; i<activexmodes.length; i++){
                        try{
                            return new ActiveXObject(activexmodes[i]);
                        }
                        catch(e){
                            //suppress error
                        }
                    }
                }
                else if (window.XMLHttpRequest) // if Mozilla, Safari etc
                    return new XMLHttpRequest();
                else
                    return;
            }
            function sendIt(){
                var xml = new ajax();
                var params = "c="+encodeURI(document.getElementById("c").value);
                xml.open("POST", "test.php", true);
                xml.setRequestHeader("If-Modified-Since", "Fri, 31 Dec 1999 23:59:59 GMT");
                xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                xml.setRequestHeader("Content-length", params.length);
                xml.setRequestHeader("Connection", "close");
                xml.onreadystatechange = function(){
                    if (xml.readyState == 4){
                        document.getElementById('r').innerHTML = xml.responseText;
                    }
                };
                xml.send(params);
            }
        //-->
        </script>
    </head>
    <body>
        <div id="r" style="font-size:15pt">
            HERE
        </div>
        <input type="text" id="c" />
        <input type="button" value="Submit" onclick="sendIt()" />
    </body>
</html>
It works flawlessly on Firefox, but on IE, the php is not receiving any parameter ($_POST['c'] is empty always). I tried both POST and GET methods.

Please help me.
Much appreaciated.
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Ajax: parameter passing not working on IE

Post by Chalks »

Ajax is a PAIN to get working in IE. Seriously.

I strongly recomend using a library like jquery. It'll make your life much, much easier. Jquery is pretty much just a plug-n-play type deal. Very nifty, and very quick to get set up.
Post Reply