PHP and Javascript work together

This may seem strange, but I programmed the games in PHP. The main problem that I discovered is that the only way to update PHP is to load the page. This slows down the work in real time. Javascript can interact with the page without reloading it. Is it possible to load PHP pages on a page using Javascript? So this will allow PHP to load again and again without rebooting.

I saw this with chat rooms, but I don’t know how it works.

+5
source share
3 answers

We mainly use Ajax, which consists of client-side Javascript code that invokes the server page without leaving the page.

, , GET (JSFiddle):

var xhr = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHttp');
xhr.onreadystatechange = function(){
    if(xhr.readyState==4 && ((xhr.status>=200 && xhr.status<300) || xhr.status==304)){//Checks if the content was loaded
        console.log(this.responseText);
    }
}
xhr.open('GET','myPHPPage.php?foo=foo&bar=bar',true);
xhr.send();

, POST (JSFiddle):

var xhr = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHttp');
var data = 'foo=foo&bar=bar';
xhr.onreadystatechange = function(){
    if(xhr.readyState==4 && ((xhr.status>=200 && xhr.status<300) || xhr.status==304)){//Checks if the content was loaded
        console.log(this.responseText);
    }
}
xhr.open('POST','myPHPPage.php',true);
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhr.setRequestHeader('Content-length',data.length);
xhr.send(data);

, setRequestHeader HTTP- Content-type Content-length ( 4096 ). , setRequestHeader open.

:

https://developer.mozilla.org/en/Ajax

http://code.google.com/intl/pt-BR/edu/ajax/tutorials/ajax-tutorial.html

+3

, . Ajax.

+2

AJAX!!! ajax

0

All Articles