Help - Search - Members - Calendar
Full Version: remember comment info
Movable Type Community Forum > Additional Resources > Tips and Tricks
jenn99
I'm using the same hack that I used for GM - only slightly adjusted for MT:

put this in your head tags:

CODE
<script TYPE="text/javascript" LANGUAGE="JavaScript">
<!--//

// Copyright (c) 1996-1997 Athenia Associates.
// http://www.webreference.com/js/
// License is granted if and only if this entire
// copyright notice is included. By Tomer Shiran.

function setCookie(name, value, expires, path, domain, secure) {
var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
document.cookie = curCookie;
}

function getCookie(name) {
var prefix = name + "=";
var nullstring = "";
var cookieStartIndex = document.cookie.indexOf(prefix);
if (cookieStartIndex == -1)
return nullstring;
var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
if (cookieEndIndex == -1)
cookieEndIndex = document.cookie.length;
return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function deleteCookie(name, path, domain) {
if (getCookie(name)) {
document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT"
};
}

function fixDate(date) {
var base = new Date(0);
var skew = base.getTime();
if (skew > 0)
date.setTime(date.getTime() - skew);
}

//-->
</SCRIPT>


CODE
add this WITHIN form tag on your comments page:
(within the " < form " " > ") - and change WWW.YOURURL.COM to your url...

CODE
NAME="comments" onSubmit="if (document.comments.bakecookie.checked){var now = new Date();fixDate(now); now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000); setCookie('gmcmtauth', document.comments.author.value, now,'','WWW.YOURURL.COM' ,''); setCookie('gmcmtmail', document.comments.email.value ,now,'','WWW.YOURURL.COM ' ,''); setCookie('gmcmthome', document.comments.url.value, now,'','WWW.YOURURL.COM ' ,''); }"


CODE
then add this BEFORE your "</form>" tag for your comments...

CODE
<p><input TYPE="checkbox" NAME="bakecookie" class="inputplain">
<label for="bakecookie">Remember info?</label></p>


CODE
AFTER your </form> tag for the comment form... add this...
(change WWW.YOURURL.COM...)

CODE
<FORM name="eatcookie" onSubmit="deleteCookie('gmcmtmail','','WWW.YOURURL.COM' );deleteCookie('gmcmthome','','WWW.YOURURL.COM' );deleteCookie('gmcmtauth','','WWW.YOURURL.COM' );document.comments.email.value = '';document.comments.author.value = '';document.comments.url.value = '';"><br>
<INPUT TYPE=SUBMIT VALUE="Forget personal information">
</form>
<script TYPE="text/javascript" LANGUAGE="JavaScript">
<!--//
document.comments.email.value    = getCookie("gmcmtmail");
document.comments.author.value   = getCookie("gmcmtauth");
document.comments.url.value = getCookie("gmcmthome");
//-->
</SCRIPT>
fooljay
Jenn, this looked so cool that I wrote about it here, but unfortunately, although the cookie is set, the data is not populated in the form upon refresh or revisit.  

Don't you need some sort of VALUE setting in the input fields in order to populate the form fields with the cookie data?  The script at the end doesn't seem to be doing its job...

Help!!
briank
This is a really useful feature that ought to be incorporated into the next major upgrade of MT.

It's worth mentioning that the first code sample (the initial javascript) goes in the head tag of your comment preview template, not the head tag of your main index template.  It took me a few false starts to figure that out because it's not explicitly mentioned the way the others are.

Thanks, Jenn (and thanks to fooljay for mentioning it on his site).
badpoetry
I agree that this would be an extremely helpful MT feature. Any chances of a comment cookie feature in future versions, Ben & Mena? Please?
adampsyche
Update:

   CGI::Cookie...
       Your server has CGI::Cookie installed.

Got that after running cgi-check.cgi (duh on me!)

Still not working, though.
silvercube
I've been working on a PHP/JavaScript hybrid "Remember me" function, which uses the common JavaScript code and some PHP to get around some JavaScript bugs. (One of these is a bug in IE5/Mac. When the form auto-fills, it appears as if some of the data values are truncated. They're not--scrolling left in the field reveals the full value. As far as I can tell, this is a browser bug. Doesn't happen anywhere else.)

Anyhow, the code is in alpha. If anyone's interested, drop me a note. It's a little harder to implement than the built-in MT code, but in my testing, it works a little better. Once it's to a level where I'm satisfied with it, I'll do a general release with full documentation.

And by the way: if stuff doesn't seem to be filling in right, try closing the current browser window, opening a new one, and navigating to the page in question. For me, that helps 99.9% of the time. (In most configurations, this forces the browser to check the server instead of relying on the cached copy.) If that doesn't work, make sure your browser is set up to handle cookies correctly.
Nikki
Greymatter has a hack where a visitor can choose to select "Remember Me" when commenting. Obviously this way the visitor doesn't have to keep reentering his/her name/email/url when commenting.

Is there a way to do this for MT?

TIA
~ Nikki
Nikki
Jenn, you rock.

Gratefully,
~ Nikki
fooljay
Nevermind, I think I see what the problem is.  My PHP code is not being executed from the CGI.  I have to hard code in the domain values.  *sigh*

An additional note for those not familiar with javascript.  If you want that cookie to be available throughout your site and not just for pages in that directory, change the parts of the form tag addition above that look like this:
CODE
now,'','WWW.YOURURL.COM'

to this:
CODE
now,'/','WWW.YOURURL.COM'

Adding the slash between the single quotes sets the cookie path to the root level.  I have different blogs in /web, /journal, /tooltalk.  The way the example was set up before, the information would only be remembered on a per directory basis.  With this change, it's no available to all three sections.
btrott
Yep, we're planning on integrating this JS into the default templates for the next release. JS is really the only way to do it, because the pages are static (unless you're using popup comments, and even then, the forms themselves are static, because they're templates)--so that's what we'll do.
adampsyche
My "remember me" script does not seem to work. I copied and pasted all of the code from the new templates, but was told that I may need a module in my extlib dir to make the cookies work? I was wondering if you have any information about this. I also tried to make a new test blog and use the remember me script (wanted to make sure I got all the code), but that didnt' work either. Is there a mod that I need?
btrott
If you check the cookies in your browser, are the relevant cookies getting set? Are you getting errors? If so, what errors are you getting?
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.