[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

YUI Cookie to remember previous form information

Posted on 2011-10-03
5
Medium Priority
?
411 Views
Last Modified: 2012-05-12
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
Comment
Question by:DSTIS1
  • 3
  • 2
5 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 36910373
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
 

Author Comment

by:DSTIS1
ID: 36914498
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
 
LVL 83

Accepted Solution

by:
leakim971 earned 1000 total points
ID: 36914502
yes, you miss to add the pluginS
0
 

Author Comment

by:DSTIS1
ID: 36921925
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
 
LVL 83

Expert Comment

by:leakim971
ID: 36922177
you're welcome, thanks you for the points, see you later
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month20 days, 1 hour left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question