• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 418
  • Last Modified:

YUI Cookie to remember previous form information

Hello,
I am working on a search form with a variety of form fields and I need a yui2 cookie that will display the form's previous state when the user returns to the page....ie, text they entered in the fields, radio buttons selected, and pulldowns selected.

Can anyone provide a solution?
0
DSTIS1
Asked:
DSTIS1
  • 3
  • 2
1 Solution
 
leakim971PluritechnicianCommented:
Here a basic example handling only textbox fields : http://jsfiddle.net/vYdpx/1/
<form id="myform" action="http://www.google.com">
    <input type="text" name="textbox1" />
    <input type="text" name="textbox2" />
    <input type="submit" value="Submit" />
</form>

Open in new window


Javascript :
function init() {
    var fields = YAHOO.util.Cookie.get("myform");
    if(fields!=null) {
        fields = fields.split("&")
        for(var i=0;i<fields.length;i++) {
            var name = fields[i].split("=")[0];
            var value = decodeURIComponent(fields[i].split("=")[1]);
            YAHOO.util.Selector.query("input[name='" + name + "']")[0].value = value;
        }
    }
    YAHOO.util.Event.addListener("myform", "submit", fnCallback);
}
YAHOO.util.Event.onDOMReady(init);

function fnCallback(e) {
    //YAHOO.util.Event.preventDefault(e);
    var fields = YAHOO.util.Connect.setForm(document.getElementById("myform"));
    YAHOO.util.Cookie.set("myform", fields, {expires: new Date("January 12, 2025"), path:location.pathname});
}

Open in new window


Ressources :

http://developer.yahoo.com/yui/cookie/
http://developer.yahoo.com/yui/event/
http://developer.yahoo.com/yui/connection/
http://developer.yahoo.com/yui/selector/
0
 
DSTIS1Author Commented:
Thank you for replying!

I tried setting up the example posted but couldn't get it to remember my information when I came back to the page......I may have not set it up correctly,

I first tried on a my local machine but couldn't get it to work.....so thinking the files might need to be on a server, tonight I uploaded them but I didn't see a difference there either.

Using the browser's back button will show the previous entered information, but I think firefox does that anyway........so I linked it to a html page that has a link back to the form and upon returning, the form fields are blank.

I took a look using the web development plugin tool for firefox to see if I could view the cookie but it didn't show anything.

Again, I could have missed something in setting it up.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function init() {

    // check if a cookie exists
    var fields = YAHOO.util.Cookie.get("myform");
    // if a cookie exists
    if(fields!=null) {
        // split by & to create an array of key+value of fields
        fields = fields.split("&")
        // for each key+value
        for(var i=0;i<fields.length;i++) {
            // get the name (left part)
            var name = fields[i].split("=")[0];
            // get the value (right part) and decode it
            var value = decodeURIComponent(fields[i].split("=")[1]);
            // set back value based on name of the field
            // for radio button, checkbox and textarea you will need to perform some additional tasks
            YAHOO.util.Selector.query("input[name='" + name + "']")[0].value = value;
        }
    }

    // add a listener to capture submit of the form
    YAHOO.util.Event.addListener("myform", "submit", fnCallback);
}
YAHOO.util.Event.onDOMReady(init);

function fnCallback(e) {
    // serialize the form (for each field key=value)
    var fields = YAHOO.util.Connect.setForm(document.getElementById("myform"));
    // save the parameters string (form serialized) in a cookie
    YAHOO.util.Cookie.set("myform", fields, {expires: new Date("January 12, 2025"), path:location.pathname});
}
</script>
</head>

<body>
<form id="myform" action="http://www.google.com">
    <input type="text" name="textbox1" />
    <input type="text" name="textbox2" />
    <input type="submit" value="Submit" />
</form>
</body>
</html>

Open in new window

0
 
leakim971PluritechnicianCommented:
yes, you miss to add the pluginS
0
 
DSTIS1Author Commented:
Thanks......I added the yui dependences, put it on a server and it did drop a cookie in my browser.

This will be a good starting point until we can test more on a live server.

Thanks again!
0
 
leakim971PluritechnicianCommented:
you're welcome, thanks you for the points, see you later
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now