[AJAX] xhr.status = 0, Mais pas toujours :~
Bonjour,
J’ai fait une page web qui affiche le contenu du répertoire courant. Je viens d’y ajouter une fonction qui me permet d’ajouter un nouveau dossier.
Pour créer se dossier j’utilise une fonction qui récupère le nom ultérieurement saisie par l’utilisateur et cette fonction ouvre un fichier PHP (avec le nom du dossier envoyer par POST) qui créé le dossier.
Problème : la fonction marche une foi sur 5 (environ). Quand la fonction ne marche pas xhr.status est égale à 0.
Voici le code de la page principal « index.php »:
Code:
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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>Mon exploreur</title>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=iso-8859-15" />
<link href="style.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript">
//POUR LE MOMENT LE FICHIER EST AJOUTER UNTIQUEMENT DANS LE MÊME REPERTOIR
//QUE "ajout.php"
var xhr;
if (window.XMLHttpRequest)
xhr = new XMLHttpRequest();
else if (window.ActiveXObject)
xhr = new ActiveXObject("Microsoft.XMLHTTP");
else
alert("!! ERREUR !!");
//ajout d'un dossier
function ajout_dir(){
var nom_dir = document.getElementById("nom_dossier").value;
var donnee;
//ouverture du fichier
xhr.open("POST", "ajout.php", true);
//test erreur
xhr.onreadystatechange = handleAJAXReturn;
xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
donnee = "nom="+nom_dir;
xhr.send(donnee);
document.location.reload();
}
//fonction test si tout marche bien...
function handleAJAXReturn(){
if (xhr.readyState == 4)
{
if (xhr.status == 200)
alert("effectuer : "+xhr.responseText);
else
alert("status != 200 => "+xhr.status);
}
else{
alert("readyState != 4 => "+xhr.readyState)
}
}
//affiche formulaire ou pas...
function secret(secretId)
{
if(document.getElementById)
{
secret = document.getElementById(secretId);
if(secret.style.display == "none")
{
secret.style.display = "";
}
else{
secret.style.display = "none";
}
}
}
</script>
</head>
<body>
<div id="menu">
<a href="" onclick="global:secret('boite_nv_dossier');return false;">ajouter nv dossier</a>
<div id="boite_nv_dossier" style="display : none; ">
<form method="POST" action="">
<label for="nom_dossier">Nom du dossier : </label>
<br />
<input type="text" id="nom_dossier" />
<br />
<a href="" onclick="javascript:ajout_dir();">Valider</a>
<a href="" onclick="global:secret('boite_nv_dossier');return false;">Annuler</a>
</form>
</div>
<br />
<br />
<a href="" id="nv_fichier" onclick="javascript:ajout_fic();">ajouter nv fichier</a>
</div>
<div id="explorateur">
<?php
///////////////TEST\\\\\\\\\\\\\\\
//echo "je veu aller ici : ".$_GET['getcwd']."\\".$_GET['dir']."<br />";
///////////////TEST\\\\\\\\\\\\\\\
//si on ré-execute le script
if ($_GET['dir'])
//On change de répertoir
chdir($_GET['getcwd']."\\".$_GET['dir']);
//on récupére le contenu du répertoir
$dir = dir(getcwd());
///////////////TEST\\\\\\\\\\\\\\\
//echo "chemin absolut : ".getcwd()."<br /><br />";
///////////////TEST\\\\\\\\\\\\\\\
//nous parcourons le répertoir tant qu'il y a un fichier
//quand tout les fichiers on était lu read() retourne FALSE
while ($nom = $dir->read()){
//affichage du nom des fichier
//et des dossiers avec lien
if(is_dir($nom))
echo "<a href=\"?dir=".$nom."&getcwd=".getcwd()."\">".$nom."</a> <br />";
else
echo $nom."<br />";
}
$dir->close();
?>
</div>
<!-------------- ZONE DE TEST -------------->
<div id="test">
ZONE TEST
<br />
<input type="text" id="resultat" />
</div>
</body>
</html> |
Et là le code de la page « ajout.php » :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
<?php
//ajout d'un fichier ou d'un dossier
$nom = $_POST['nom'];
///////////////TEST\\\\\\\\\\\\\\\
$h = fopen ("ERREUR.txt", "a+");
fwrite ($h, "\n Nom : ".$nom."\n");
///////////////TEST\\\\\\\\\\\\\\\
$res = mkdir($nom);
///////////////TEST\\\\\\\\\\\\\\\
if ($res == true)
fwrite ($h, "Operation reussite\n");
else
fwrite ($h, "ERREUR dossier non cree\n");
///////////////TEST\\\\\\\\\\\\\\\
fclose($h);
?> |
Merci d’avance pour votre aide.