Salve a tutti ho grossi problemi con il fantastico mondo di ajax, premesso che in materia sono un neofita, e stō studiando l'argomento da un libro tradotto dall'inglese all'italiano, l'argomento dell'ultimo capitolo č ajax con il php, in particolar modo con l'elemento DOM, qui di seguito riporto il listato dei tre file :
1) l'interfaccia html, il file phptest.html
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>AJAX: PHP DOM</title>
<script type="text/javascript" src="phptest.js"></script>
</head>
<body onload="process()">
Il libro di ajax 2006 :
<div id="myDivElement" />
</body>
</html>
2) il motore js phptest.js
//l'istanza XMLHttpRequest
//creo l'istanza dell'oggetto XMLHttpRequest
var xmlHttp = createXMLHttpRequestObject();
function createXMLHttpRequestObject()
{
//memorizza il riferimento dell'oggetto XMLHttpRequest
var xmlHttp;
//x tutti i browser eccetto IE6
try
{
xmlHttp = new XMLHttpRequest();
}
catch( e )
{
var xmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
"MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0",
"MSXML2.XMLHTTP","Microsoft.XMLHTTP");
//adesso provo tutti gli elementi dell'array finchč nn ne trovo quello giusto
for (var i=0; i < xmlHttpVersions.length && !xmlHttp; i ++)
{
try
{
xmlHttp = new ActivexObject(xmlHttpVersions[i]);
}
catch( e ) {}
}
}
if( !xmlHttp )
alert("Err!!!!! during the creation XMLHttpRequestObject");
else
return xmlHttp;
}
function process()
{
if( xmlHttp )
{
try
{
xmlHttp.open("GET", "phptest.php", true);
xmlHttp.onreadystatechange = handleRequestStateChange;
xmlHttp.send( null );
}
catch( e )
{
alert("Err!!!! collegamento al server:\n" +e.toString());
}
}
}
function handleRequestStateChange()
{
//quando readystate č in posizione 4 siamo pronti per leggere la risposta
if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)//TEST
{
try
{
handleServerResponse();
}
catch( e )
{
alert("errore durante la lettura della risposta:"+ e.toString());
}
}
else
{
alert("Si sta verificando un errore sul server nella ricezione dei dati:\n" + xmlHttp.statusText);
}
}
}
//gestione risposta ricevuta dal server
function handleServerReponse()
{
var xmlResponse = xmlHttp.responseXML;
//cattriamo eventuali erroricon firefox
//valutiamo eventuali errori con opera e ie6
if (!xmlResponse || !xmlResponse.documentElement)
throw ("Struttura XML non valida:\n" + xmlHttp.response.Text);
//catturiamo eventuali errori
var rootNodeName = xmlResponse.documentElement.nodeName;
if (rootNodeName == "parsererror") throw ("Strutt XML non valida(12)");
//otteniamo il documento radice dal file xml
xmlRoot = xmlResponse.documentElement;
//otteniamo gli array con i titoli dei libri e tutti gli altri dati
titleArray = xmlRoot.getElementsByTagName("title");
isbnArray = xmlRoot.getElementsByTagName("isbn");
//output HTML
var html="";
//iterazione degli elementi per l'output hml
for(var i=0; titleArray.length; i++)
html += titleArray.item( i ).firstChild.data +", "+isbnArray.item( i ).firstChild.data +"
";
// otteniamo un riferimento all'elemento <div> della pagina
myDiv = document.getElementById ("myDivElement");
//mostriamo l'output in html
myDiv.innerHtml = html;
}
3) il file php phptest.php
<?
//inviamo l'output come xml
header ('Content-type: Text/xml');
//creiamo il nuovo documento XML
$dom = new DOMDocument();
//creiamo l'elemento radice <response>
$response = $dom ->createElement('response');
$dom ->appendChild($response);
//creiamo l'elemento <books> e lo concateniamo con <response>
$books = $dom ->createElement('books');
$response ->appendChild($books);
//creiamo l'elemento title del libro
$title = $dom->createElement( 'title' );
$titleText = $dom->createTextNode( 'Building Reponsive Web Applications with AJAX and PHP');
$title->appendChild($books);
//creiamo l'elemento isbn del libro
$isbn = $dom->createElement( 'isbn' );
$isbnText = $dom->createTextNode( '1-904811-82-5');
$isbn->appendChild($isbnText);
//creiamo l'elemento <book>
$book = $dom->createElement( 'book' );
$book->appendChild($title);
$book->appendChild($isbn);
//concateniamo <book> come figlio di books
$books ->appendChild($book);
//costruiamo la struttura XML in una variabile stringa
$xmlString = $dom->saveXML(); //N.B.
//restituiamo la variabile stringa
echo $xmlString;
?>
i listati li ho visti e rivisti inutile dire che ho controllato tutte le impostazioni di php, di apache, ho porvato a far girare gli script su diversi browser, (opera, firefox, google chrome, ie) ma niente![]()
![]()
![]()
![]()
![]()
![]()
Qualcuno di voi sā come aiutarmi????