简体   繁体   中英

Fill div with a php script via Ajax call?

I'm wondering what is the simplest way to fill a div by grabbing a PHP script (and send data like POST or GET) to process what data to return.

But without the use of a library... All I ever find is prototype and jquery examples.

Has any one done this without a library?

var xmlhttp;

function showUser()
{
    xmlhttp=GetXmlHttpObject();


    if (xmlhttp==null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var url="yourpage.php";
    url=url+"?q="+str;

    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
}

function stateChanged()
{
    if (xmlhttp.readyState==4)
    {
        document.getElementById("yourdiv_id").innerHTML=xmlhttp.responseText;
    }   
}

function GetXmlHttpObject()
{
  if (window.XMLHttpRequest)
  {
    return new XMLHttpRequest();
  }
  if (window.ActiveXObject)
  {
  // code for IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
    return null;
}   

I'd suggest you look at this code .

You will see how to handle perfectly XHR objects, and it also gives you some sugar syntax.

Btw, a less-than-one-hundred-lines "library" should be fine, since it basically does what you want it to, and no more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM