www.webdeveloper.com
Results 1 to 4 of 4

Thread: Writing A Cookie Based On Breeze Form Entry

Hybrid View

  1. #1
    Join Date
    Mar 2013
    Posts
    24

    Question Writing A Cookie Based On Breeze Form Entry

    Newbie question:

    I need to write a cookie based on the value of a radio button form entry.

    I'm using Breeze forms to create the radio button form. It allows for custom "action scripts" to be executed on submit events.

    I have the javascript to set the cookie. I have the action script that returns the radio button input. Questions is how do I merge them together to write the cookie according to the radio entry.

    Here's the action script:
    (Gets the value of the currently checked radiobutton.)


    Code:
    function ff_checkedValue(name)
    {
        // loop through elements and find checked
        for (i = 0; i < ff_elements.length; i++)
            if (ff_elements[i][2]==name) {
                e = ff_getElementByIndex(i);
                if (e.checked) return e.value;
            } // if 
        return '';
    } // ff_checkedValue

    Here's the cookie writing script:

    Code:
    script type = "text/javascript">
    var expires = new Date();
    expires.setDate(expires.getDate() + 365); // 365 = days to expire
    document.cookie = "radio_1=[WHAT TO PUT HERE??];expires="+ expires.toGMTString()+"; path=/; domain=localhost";
    </script>
    What's the code that would write the cookie "radio_1" to the value that the action script returns?

    Any help would be much appreciated here.

  2. #2
    Join Date
    Mar 2009
    Posts
    467
    I would suggest that you simply use a single JSON string representation of an array to store the entire set, or even better since this is a set of radio buttons, the one that is checked. For example, if you have three radio buttons in a set and the middle is checked then: [false, true, false] or, as the second alternative, an object such as {checked: 1} to represent that the second one is checked.

  3. #3
    Join Date
    Mar 2013
    Posts
    24
    Thanks Tcobb. I'm no programmer. I'm trying to bootstrap by hacking existing code here. My mistake. But right now, it's what's gotta be done.

    Do you have the code that would do that? Or where I can get it? And will this solution work within the Breeze forms framework? Meaning I can set a script to execute once the radio button is selected (but have to use the function ff_checkedValue to get the actual value of the radio selection).

  4. #4
    Join Date
    Mar 2009
    Posts
    467
    I know nothing about "Breeze" at all. But my question at this point is why you need to write this cookie:
    (1) for user analytics on the server side
    (2) to restore state on the client side
    (3) both
    (4) none of the above

    You might try something like the following. It is untested.:

    Code:
    //args should be the "name" of the set of radio buttons and cont should
    //be their parent container(reference, not ID) if known--otherwise
    // don't use a second argument or you may regret it
    //DON'T run this until the Breeze object has come into being
    //It ASSUMES the existence of a global function named 'setCookie',
    //which receives a string which represents the entire cookie to be written.
    // if the "name" of the set was 'fred' and the second button was checked
    // it would get 'fred=1' as its argument
    
    function armCookie(name, cont){
        var inp, rad, i, len, sh, fn, remora, cookie;
        //if container is unknown, search the entire document
        if(cont === undefined){
    	cont = document.body;
        }
    
        rad = [];
        inp = cont.getElementsbyTagName('input');
        len = inp.length;	
        //identify all radio buttons with that 'name' and place them in 'rad'
        for(i = 0; i < len; i++){
    	sh = inp[i];
    	if(sh.type && sh.type == 'radio' && sh.name && sh.name == name){
    	    rad.push(sh);
    	}
        }
    
        //create a function to attach to the buttons
        remora = function(arr, name){
    	return function(){
    	    var i, len, str, item = null;
    	    len = arr.length;
    	    for(i = 0; i < len; i++){
    		if(arr[i].checked === true){
    		    item = i;
    		    break;
    		}
    	    }
    	    str = name + '=' + item;
    	    //assumes the existence of a global function called setCookie
    	    //you write this
    	    setCookie(str);
    	}(rad, name);
        }   
    
        //now set up these buttons so the cookie will be (re)written every time
        //one is checked/unchecked
    
        len = rad.length;
        for(i = 0; i < len; i++){
    	fn = rad[i].onclick;
    	if(typeof(fn) == 'function'){
    	    rad[i].onclick = function(a,b,c,d,e,f){
    		var z = fn(a,b,c,d,e,f);
    		remora();
    		return z;
    	    }
    	}
    	else{
    	    rad[i].onclick = remora;
    	}
        }
    }

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