J'up le post pour préciser que même en faisant un alert j'arrive pas à obtenir la moindre donnée.
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
<html>
<head>
<script type="text/javascript">
function cleanXML(d){
var bal=d.getElementsByTagName('tableau2');
for(i=0;i<bal.length;i++){
a=bal[i].previousSibling;
if(a && a.nodeType==3)
go(a);
b=bal[i].nextSibling;
if(b && b.nodeType==3)
go(b);
}
return d;
}
function getXMLHttpRequest() {
var xhr = null;
if(window.XMLHttpRequest || window.ActiveXObject) {
if(window.ActiveXObject) {
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
} else {
xhr = new XMLHttpRequest();
}
} else {
alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
return null;
}
return xhr;
}
function ReqAjax(id) {
var xhr = getXMLHttpRequest();
if(xhr && xhr.readyState != 0) {
xhr.abort();
}
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0))
{
oData = xhr.responseXML;
//response = cleanXML(xhr.responseXML);
affichTab(oData);
} else if(xhr.readyState == 2 || xhr.readyState == 3) {
document.getElementById("loading").innerHTML = "<p>Chargement en cours</p>";
}
}
//xhr.open("GET", "testPhp.php", true);
xhr.open("GET", 'testPhp.php?id="+id"', true);
xhr.send(null);
}
function affichTab(oData)
{
var taille = oData.getElementById('tabl')[0].attributes.length;
alert("ok: "+taille);
//var taille = oData.getElementsById("tab").length;
/*
var oTabl = oData.getElementsByTagName("table");
var oTableau2 = document.getElementById("tableau2");
var oTr = oData.getElementsByTagName("tr");
var oTd = oData.getElementsByTagName("td");
var oTabl2 = document.createElement("table");
var bordureTab = document.createAttribute("border");
bordureTab.nodeValue = "4";
oTabl2.setAttributeNode(bordureTab);
var oTr2 = document.createElement("tr");
var oTd2 = document.createElement("td");
oTd2.value = oTd.nodeValue;
oTableau2.appendChild(oTabl);
oTabl.appendChild(oTr);
oTr.appendChild(oTd);
*/
}
</script>
</head>
<body>
<table id="superTableau" border=2px>
<thead>
<tr>
<th>[id]</th>
<th>possesseur</th>
</tr>
</thead>
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('ajax');
$query = mysql_query('SELECT id FROM jeux_video LIMIT 0,10');
while($back = mysql_fetch_assoc($query))
{
$id = $back['id'] ;
echo '<tr onclick="ReqAjax('. $id .');" >';
echo '<td>' . $back['id'] . '</td>';
echo '<td>?</td>';
echo '</tr>';
}
?>
</table>
<div id="tableau2">
</div>
</body>
</html> |
Ya t-il un moyen de voir si j'ai bien des données dans oData? (voir fonction affichTab)
edit: oui il y a firebug...
Bonjour,
Une question simple sur laquelle je perds du temps.
Faire apparaitre un second tableau (à peut près ok) qui récupère une valeur (le problème!) provenant d'une bdd.
Voici le code:
test.php
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
<html>
<head>
<script type="text/javascript">
function getXMLHttpRequest() {
var xhr = null;
if(window.XMLHttpRequest || window.ActiveXObject) {
if(window.ActiveXObject) {
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
} else {
xhr = new XMLHttpRequest();
}
} else {
alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
return null;
}
return xhr;
}
function ReqAjax(id) {
var xhr = getXMLHttpRequest();
if(xhr && xhr.readyState != 0) {
xhr.abort();
}
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0))
{
//oData = xhr.responseXML;
oData = cleanXML(xhr.responseXML);
affichTab(oData);
} else if(xhr.readyState == 2 || xhr.readyState == 3) {
document.getElementById("loading").innerHTML = "<p>Chargement en cours</p>";
}
}
xhr.open("GET", "testPhp.php?id="+id, true);
xhr.send(null);
}
function affichTab(oData)
{
var oTabl = oData.getElementById("tabl");
var oTableau2 = document.getElementById("tableau2");
var oTr = oData.getElementsByTagName("tr");
var oTd = oData.getElementsByTagName("td");
var oTabl2 = document.createElement("table");
var bordureTab = document.createAttribute("border");
bordureTab.nodeValue = "4";
oTabl2.setAttributeNode(bordureTab);
var oTr2 = document.createElement("tr");
var oTd2 = document.createElement("td");
//oTd2.value = oTd.nodeValue;
[#red]oTd2.nodeValue = oTd.data;
oTableau2.appendChild(oTabl2);
oTabl2.appendChild(oTr2);
oTr2.appendChild(oTd2);
}
</script>
</head>
<body>
<table id="superTableau" border=2px>
<thead>
<tr>
<th>id</th>
<th>possesseur</th>
</tr>
</thead>
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('ajax');
$query = mysql_query('SELECT id FROM jeux_video LIMIT 0,10');
while($back = mysql_fetch_assoc($query))
{
$id = $back['id'] ;
echo '<tr onclick="ReqAjax('. $id .');" >';
echo '<td>' . $back['id'] . '</td>';
echo '<td>?</td>';
echo '</tr>';
}
?>
</table>
<div id="tableau2">
</div>
</body>
</html> |
testPhp.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
<?php
header("Content-Type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>";
echo '<table id="tabl">';
if(isset($_GET['id'])) {
$id = ($_GET['id']);
mysql_connect('localhost', 'root', '');
mysql_select_db('ajax');
$query = mysql_query('SELECT possesseur FROM jeux_video WHERE id=' . $id);
while($back = mysql_fetch_assoc($query))
{
echo "<tr>";
echo "<td>" . $back['possesseur'] . "</td>";
echo "</tr>";
}
}
echo "</table>";
?> |
Il s'agit juste de la partie en rouge-gras qui me pose probleme. Après de multiples essaies je n'arrive toujours pas à récupérer la valeur.
Donc l'affichage du tableau se fait dans la fonction afficheTab avec du DOM et les infos proviennent du fichier testPhp.php.
Merci [smile] [/#]
edit: firebug me montre bien que je récupère ceci dans la réponse
<?xml version="1.0" encoding="iso-8859-1"?><table id="tabl"><tr><td>Florent</td></tr></table>
Par contre il y a cette erreur
1 2
| document.getElementById("loading") is null
[Break on this error] document.getElementById("loading")...nnerHTML = "<p>Chargement en cours</p>"; test2.php (ligne 56) |
Partager