$_POST gives me empty string

I am using an XMLHttpRequest in my javascript file to call a PHP script sending it parameters. For some reason the parameters are being “lost”. I used Firebug and have determined that the parameters are being sent, but when I print them in the PHP script they are blank. Here is my code:

function saveData()
{
    url = "saveEvents.php"

    var outputData = "TESTING OUTPUT";
        
    xmlhttp = false;
    
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch(e) {
            xmlhttp = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
           try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
          } catch(e) {
            try {
                  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                  xmlhttp = false;
            }
        }
    }
    
    if(xmlhttp) {

        xmlhttp.open("POST", url, true); 

        //Send the proper header information along with the request

        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-length", outputData.length);
        xmlhttp.setRequestHeader("Connection", "close");    

        xmlhttp.onreadystatechange = function() {//Call a function when the state changes.
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            alert(xmlhttp.responseText);
        }           

        xmlhttp.send("data=" + outputData);
    }
}

[php]

<?php $File = "events.txt"; $Handle = fopen($File, 'w'); $Data = $_POST['data']; fwrite($Handle, $Data); print "Data Added: data=".$Data; fclose($Handle); ?>

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service