www.webdeveloper.com
Results 1 to 3 of 3

Thread: Saving user preferences in a cookie

Hybrid View

  1. #1
    Join Date
    Sep 2010
    Location
    Québec, Canada
    Posts
    11

    Question Saving user preferences in a cookie

    Hey all! I'm having a few problems with this script:

    - the third text input in the form is requesting for the user's favorite color. I need it to validate the data to either be a defined color or a hexadecimal value and then turn the background into that color upon submission. However, it's not validating correctly... When a correct value is entered it works fine, it's the error message for an invalid entry that's not appearing.

    - I need to save all of the user's input into a cookie which will automatically load their preferences the next time they visit the site(time of day message, first & last name and bg color).

    Ideas?

    Here's what I have so far:

    Code:
    <html>
    <head>
    <script language="javascript">
    
    <!-- Début
    
    //Valider formulaire
    function valider(){
    
    if (document.testform.Prenom.value=="") {
    alert("S.V.P. indiquez votre prénom!")
    return false;
    }
    if (document.testform.Nom.value=="") {
    alert("S.V.P. indiquez votre nom de famille!")
    return false;
    }
    if (document.testform.Couleur.value=="") {
    alert("S.V.P. indiquez votre couleur préféré!")
    return false;
    }
    
    var inputField = document.getElementById("Couleur").value.substr(1);
    if (!/^[a-f0-9]{3}$|^[a-f0-9]{6}$/i.test(inputField))
    {
    	alert("Vous devez indiquez une valeur hexidécimale ou une couleur définie!");
    return false;
    }
    return true;
    }
    
    //Change la couleur d'arrière plan
    function changeBg(){
      var PrNom = document.forms[0].Prenom.value;
      var NomF = document.forms[0].Nom.value;
      var bgColor = document.testform.Couleur.value;
      
      //Message de bienvenue en fonction de l'heure
      var heure=new Date().getHours()
      var msg=""
      if (heure<=6) {msg="Bonne nuit"}
      if (heure>6 && heure<=12) {msg="Bon matin"}
      if (heure>12 && heure<=18) {msg="Bonne après-midi"}
      if (heure>18 && heure<=24) {msg="Bonne soirée"}
    
      document.write(msg +" " +PrNom +" " +NomF +"!");
      document.bgColor = bgColor;
     }
    
    // Fin -->
    </SCRIPT>
    </head>
    <body>
    
    <form name="testform" onSubmit="changeBg()" />
    <br />
    Pr&eacute;nom?&nbsp;<input type="text" size=30 name="Prenom"  value="Visiteur" onBlur="valider()"<br />
    Nom?&nbsp;<input type="text" size=30 name="Nom" value="Inconnu" onBlur="valider()" /><br />
    Couleur pr&eacute;f&eacute;r&eacute;?&nbsp;<input type="text" size=30 name="Couleur" value="#FFFFFF" onBlur="valider()" maxlength="7" /></br />
    <input type="submit" value="Valider">
    </form>
    </body>
    </html>

  2. #2
    Join Date
    Jun 2007
    Posts
    667
    You're calling document.getElementById for an element with no ID.

    Use the error console.

  3. #3
    Join Date
    Sep 2010
    Location
    Québec, Canada
    Posts
    11

    Question Bg color cookie problems...

    Ok, so I fixed the missing ID and seem to have a portion of my cookies working (in Explorer but not Mozilla?). How do I save the background color entered in the 3rd text field as a cookie as well?

    Code:
    <html>
    <head>
    
    <script language="javascript">
    
    <!-- Début
    
    //Valider formulaire
    function valider(){
    
    var bgColor = document.testform.Couleur.value;
    document.bgColor = bgColor;
    
    if (document.testform.Prenom.value=="") {
    alert("S.V.P. indiquez votre prénom!")
    return false;
    }
    if (document.testform.Nom.value=="") {
    alert("S.V.P. indiquez votre nom de famille!")
    return false;
    }
    if (document.testform.Couleur.value=="") {
    alert("S.V.P. indiquez votre couleur préféré!")
    return false;
    }
    
    var inputField = document.getElementById("Couleur").value.substr(1);
    if (!/^[a-f0-9]{3}$|^[a-f0-9]{6}$/i.test(inputField))
    {
    	alert("Vous devez indiquez une valeur hexidécimale!");
    return false;
    }
    return true;
    }
    
    function set_it() {
    
      var inputFieldP = document.getElementById("Prenom").value
      var inputFieldN = document.getElementById("Nom").value
      var thetext="greeting=Bonjour "+inputFieldP +" " +inputFieldN +",";
      var expdate=";expires=Mon, 30 Mar 2020 13:00:00 UTC";
      var newtext=escape(thetext);
      newtext+=expdate;
      document.cookie=newtext
      
    }
    
    function read_it() {
    
     var mycookie =document.cookie;
     var fixed_cookie = unescape(mycookie);
     var thepairs = fixed_cookie.split(";");
     var namepairs= thepairs[0].split("=");
     var greet= namepairs[1];
     document.write(greet);
     }
    
    if (document.cookie) {
      read_it();
    }
    else {
      set_it();
    }
    
    // Fin -->
    </script>
    </head>
    
    <body>
    <script language="javascript">
    
    //Message de bienvenue en fonction de l'heure
    var heure=new Date().getHours()
    var msg=""
    if (heure<=6) {msg="bonne nuit"}
    if (heure>6 && heure<=12) {msg="bon matin"}
    if (heure>12 && heure<=18) {msg="bonne après-midi"}
    if (heure>18 && heure<=24) {msg="bonne soirée"}
      
    document.write(msg +"!");  
      
    </script>
    
    <form name="testform" >
    Prénom:&nbsp;<input size="30" name="Prenom" value="Visiteur" onblur="set_it" type="text"><br>
    Nom:&nbsp;<input size="30" name="Nom" value="Inconnu" onblur="set_it" type="text"><br>
    Couleur préféré:&nbsp;<input size="30" name="Couleur" value="#FFFFFF" onblur="set_it()" maxlength="7" type="text"><br>
    <input value="Valider" type="button" onClick="valider()">
    <input value="Soumettre" type="button" onClick="read_it">
    </form>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles