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?
DSTIS1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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/
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

leakim971PluritechnicianCommented:
yes, you miss to add the pluginS

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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!
leakim971PluritechnicianCommented:
you're welcome, thanks you for the points, see you later
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.