Tuesday, August 7, 2012

Javascript Ajax POST to a html id function


I found and modified this a few months back so I could POST information into a page's div id's.
////////////////////////////////////
///////////AJAX/////////////
/////////////////////////////////////
//url is the target page
//tar is the div target the post is going to
//val is the array being sent. It must be in this format val = "key='value'&key2='value2'";
function PostToDiv(url, tar, val)
{
    var xmlhttp;
    var c = 0;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
   
    xmlhttp.open("POST", url, true);
    xmlhttp.onreadystatechange=function()
    {
        if(xmlhttp.readyState==4 && xmlhttp.status==200)
        {
           
            document.getElementById(tar).innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send(val);
}

No comments:

Post a Comment