Bonjour tout le monde,

J'ai récupéré un exemple d'AJAX utilisant XML sur internet afin d'essayer de comprendre l'approche XML d'ajax (je n'ai utilisé que du texte jusque maintenant).

J'ai donc ce code HTML :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<html>
<head>
<script src="xml.js"></script>
</head>
<body>
 
<form> 
	Select a User:
	<select name="users" onchange="showUser(this.value)">
		<option value="1">Peter Griffin</option>
		<option value="2">Lois Griffin</option>
		<option value="3">Glenn Quagmire</option>
		<option value="4">Joseph Swanson</option>
	</select>
</form>
 
<h2><span id="firstname"></span>
&nbsp;<span id="lastname"></span></h2>
<span id="job"></span>
<div style="text-align: right">
<span id="age_text"></span>
<span id="age"></span>
<span id="hometown_text"></span>
<span id="hometown"></span>
</div>
 
</body>
</html>
ce code Javascript :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var xmlHttp;
 
function showUser(str)
 { 
 	 //alert(str);
	 xmlHttp=GetXmlHttpObject();
	 if (xmlHttp==null)
	  {
		  alert ("Browser does not support HTTP Request");
		  return;
	  } 
	 var url="responsexml.php";
	 url=url+"?q="+str;
	 url=url+"&sid="+Math.random();
	 xmlHttp.onreadystatechange=stateChanged;
	 xmlHttp.open("GET",url,true);
	 xmlHttp.send(null);
 }
 
function stateChanged() 
{ 
	//alert(xmlHttp.readyState);
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		 xmlDoc=xmlHttp.responseXML;
		 document.getElementById("firstname").innerHTML=
		 xmlDoc.getElementsByTagName("firstname")[0].childNodes[0].nodeValue;
		 document.getElementById("lastname").innerHTML=
		 xmlDoc.getElementsByTagName("lastname")[0].childNodes[0].nodeValue;
		 document.getElementById("job").innerHTML=
		 xmlDoc.getElementsByTagName("job")[0].childNodes[0].nodeValue;
		 document.getElementById("age_text").innerHTML="Age: ";
		 document.getElementById("age").innerHTML=
		 xmlDoc.getElementsByTagName("age")[0].childNodes[0].nodeValue;
		 document.getElementById("hometown_text").innerHTML="<br/>From: ";
		 document.getElementById("hometown").innerHTML=
		 xmlDoc.getElementsByTagName("hometown")[0].childNodes[0].nodeValue;
	 }
} 
 
function GetXmlHttpObject()
 { 
	 var objXMLHttp=null;
	 if (window.XMLHttpRequest)
	  {
		  objXMLHttp=new XMLHttpRequest();
	  }
	 else if (window.ActiveXObject)
	  {
		  objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	 return objXMLHttp;
 }
Le fichier s'appelle xml.js

et le fichier php :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
header('Content-Type: text/xml');
header("Cache-Control: no-cache, must-revalidate");
//A date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
 
$q=$_GET["q"];
 
$con = mysql_connect('localhost', 'root', 'mdp');
if (!$con)
 {
	 die('Could not connect: ' . mysql_error());
 }
 
mysql_select_db("ajax_demo", $con);
 
$sql="SELECT * FROM user WHERE id = ".$q."";
 
$result = mysql_query($sql);
 
echo '<?xml version="1.0" encoding="ISO-8859-1"?>
<person>';
while($row = mysql_fetch_array($result))
 {
	 echo "<firstname>" . $row['FirstName'] . "</firstname>";
	 echo "<lastname>" . $row['LastName'] . "</lastname>";
	 echo "<age>" . $row['Age'] . "</age>";
	 echo "<hometown>" . $row['Hometown'] . "</hometown>";
	 echo "<job>" . $row['Job'] . "</job>";
 }
echo "</person>";
 
mysql_close($con);
?>
J'ai bien sûr créer la table, j'y ai mis les données, les accès...

J'obtiens ce message lors de l'exécution de mon programme :

xmlDoc.getElementsByTagName("firstname")[0] is undefined
[Break on this error] xmlDoc.getElementsByTagName("firstname")[0].childNodes[0].nodeValue;
xml.js (ligne 27)
Voici la ligne 27 :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
 xmlDoc.getElementsByTagName("firstname")[0].childNodes[0].nodeValue;
Voici l'URL du programme :

http://www.w3schools.com/php/php_ajax_responsexml.asp

Je vous remercie d'avance pour votre aide.

beegees