Sir
Sir

Reputation: 8277

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?

Upvotes: 0

Views: 415

Answers (2)

Florian Margaine
Florian Margaine

Reputation: 60787

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.

Upvotes: 0

Hkachhia
Hkachhia

Reputation: 4539

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;
}   

Upvotes: 1

Related Questions