[javascript] switch avec des strings

switch avec des strings [javascript] - HTML/CSS - Programmation

Marsh Posté le 08-10-2008 à 16:23:40    

Bonjour à tous
 
Alors voici mon probleme :
 
Je veux faire un switch afin de traiter une réponse ajax.
Par exemple en ajax quand je fais l'inscription d'un membre, je retourne "SIGNUP|OK" si c'est bien enregistré sinon je retourne "SIGNUP|ERROR".
Je fonctionne donc par systeme de mot clé (SIGNUP pour inscription, PASSLOST pour recuperer son mot de passe, etc)
 
Donc pour cela, dans mon fichier ajax.js j'ai fait cela :
 
       

Code :
  1. var tmp = xhr.responseText;
  2.         var tab = tmp.split('|');
  3.      
  4.         switch(tab[0]) //mot-clé de la réponse du fichier php
  5.         {
  6.             case "SIGNUP":             
  7.                 document.getElementById("Form_Signup" ).style.display = "none";
  8.                 if(tab[1] == "OK" )
  9.                 {
  10.                     document.getElementById("Table_Form_Signup_OK" ).style.display = "";
  11.                     document.getElementById("Table_Form_Signup_ERROR" ).style.display = "none";
  12.                 }
  13.                 else
  14.                 {
  15.                     document.getElementById("Table_Form_Signup_OK" ).style.display = "none";
  16.                     document.getElementById("Table_Form_Signup_ERROR" ).style.display = "";
  17.                 }
  18.                 break;             
  19.         }


 
Mon probleme, c'est que je ne rentre jamais dans le case "SIGNUP" alors que lorsque je fais un alert de tab[0], il y a bien ecrit SIGNUP. Donc je ne comprends pas.
 
Peut etre une erreur de syntaxe au niveau du switch ?
 
Merci d'avance

Reply

Marsh Posté le 08-10-2008 à 16:23:40   

Reply

Marsh Posté le 08-10-2008 à 17:11:33    

Fait gaffe à la casse des lettres. "SIGNUP" != "signup". Au pire fait, un :

Code :
  1. switch (tab[0].toUpperCase()) {
  2. /* ... */
  3. }

Reply

Marsh Posté le 08-10-2008 à 17:16:25    

Sinon, utiliser du json peut aussi être sympa, il te permettrait de faire des tests booléens simples genre
 

Code :
  1. /*
  2.     JSON response looks like e.g. {'signup':true} if signup worked, else {'signup':false}
  3. */
  4. var response = JSON.parse(xhr.responseText);
  5. if(response.signup) {
  6.    // was signed up
  7. } else {
  8.    // wasn't signed up
  9. }



---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 08-10-2008 à 17:38:42    

Merci pour vos réponse.
 
J'ai testé avec ta solution tpierron et effectivement cela fonctionne. J'avoue que j'aurais jamais cherché de ce coté là puisque j'écrivais pourtant tout en majuscule...
 
En tout cas merci ;)
 
--- PROBLEME RESOLU ---

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed