/    Sign up×
Community /Pin to ProfileBookmark

hi, I have a shoutbox and there is an <input> tag that is of the text type and has a name of ‘name’ and a submit button.

how can i make a cookie remeber the user’s name so that when they press the button it saves their name and the name is the default value of the <input> tag the next time the user enters the page?

thanks.

b.t.w, if it helps, this is the form:

[CODE]
<form action=”/shoutbox/sign.php” method=”GET” target=”tags”>
<div id=”Layer1″ style=”border-width: 1 1 1 1; border-color: #000000; border-style: solid; width: 100%;”>
<iframe src=”shoutbox/tags.php?show=limit” name=”tags” width=”100%” height=”200″ frameborder=”0″ ></iframe>
</div>
<div align=”center”>
<small>Shout:</small><br><input type=”text” size=”12″ name=”Shout” value=”Shout” class=”input” maxlength=”150″ onFocus=”if(this.value==’Shout’)this.value=”;”><br>
<small>Name:</small><br><input type=”text” size=”12″ name=”name” value=”Name” class=”input” maxlength=”15″ onFocus=”if(this.value==’Name’)this.value=”;”><br>
<br><input type=”submit” value=” Punch It! ” class=”button”>
</div></div></form>
[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@crh3675Feb 12.2004 — <i>
</i>
&lt;script type="text/javascript"&gt;
String.prototype.trim=function(){
return this.replace(/^s*/, '').replace(/s*$/, '');
}


function jsCookie(){
var name;
var value;
var domain;
var path;
var cookiestring;
var secure;
var expires

<i> </i>this.secure=false;
<i> </i>this.exists=false;
<i> </i>this.domain=document.domain;
<i> </i>this.path="/";
<i> </i>this.expires="";
<i> </i>this.setexpires=function(arg){

<i> </i> var exp="";

<i> </i> if (arg.match(",")){
<i> </i> newargs=arg.split(",");
<i> </i> var min=newargs[0];
<i> </i> var hour=newargs[1];
<i> </i> var day=newargs[2];
<i> </i> var year=newargs[3];

<i> </i> var minmil=60*1000;
<i> </i> var hourmil=60*(minmil);
<i> </i> var daymil=24*(hourmil);
<i> </i> var yearmil=365*(daymil);
<i> </i> var now=new Date();
<i> </i> exp=new Date(now.getTime()+(hour*hourmil)+(min*minmil)+(day*daymil)+(year*yearmil));

<i> </i> }else{
<i> </i> exp=new Date(arg);
<i> </i> }

<i> </i> this.expires=exp.toGMTString();

<i> </i>}
<i> </i>this.jsCookie=function(){
<i> </i> this.get();
<i> </i>}
<i> </i>this.set=function(){
<i> </i> var strSecure;
<i> </i> this.setexpires(this.expires)
<i> </i> strSecure=this.secure==true ? strSecure="; SECURE" : "";
<i> </i> this.cookiestring=this.name+"="+escape(this.value)+";path="+this.path+";expires="+this.expires+strSecure;
<i> </i> document.cookie=this.cookiestring;
<i> </i>}
<i> </i>this.get=function(){
<i> </i> var cookiefile=document.cookie;
<i> </i> var cookies=cookiefile.split(";");


<i> </i> for (i=0;i&lt;cookies.length;i++){
<i> </i> var thecookie=Array();
<i> </i> thecookie=cookies[i].split("=");

<i> </i> if (this.name==thecookie[0].trim()){
<i> </i> this.value=unescape(thecookie[1]);
<i> </i> return;
<i> </i> }
<i> </i> }
<i> </i>}
<i> </i>this.exists=function(){
<i> </i> var cookiefile=document.cookie;
<i> </i> var cookies=cookiefile.split(";");
<i> </i> var blnexists=false;


<i> </i> for (i=0;i&lt;cookies.length;i++){
<i> </i> var thecookie=Array();
<i> </i> thecookie=cookies[i].split("=");

<i> </i> if (this.name==thecookie[0].trim()){
<i> </i> blnexists=true;
<i> </i> break;
<i> </i> }
<i> </i> }
<i> </i> blnexists ? this.get() : '';
<i> </i> return blnexists;

<i> </i>}
<i> </i>this.destroy=function(){
<i> </i> var strSecure;
<i> </i> strSecure=this.secure==true ? strSecure="; SECURE" : "";
<i> </i> this.cookiestring=this.name+"="+escape(this.value)+";path="+this.path+";expires="+this.setexpires("0,0,0,-20");
<i> </i> document.cookie=this.cookiestring;
<i> </i>}

}


/*
####################################
JsCookie Object - written by Craig R. Hoover
#####################################

Properties:
name - name of cookie to set or retrieve
path - valid path of cookie for site (e.g. /)
domain - valid domain for cookie
secure - sets secure bit (true or false) for cookie
value - you can set or get the value of a cookie
expires - "mm/dd/yyyy" OR "minutes,hours,days,years" from current time - set your cookie expiration

Methods:
jsCookie.set() - sets all values you specify
jsCookie.get() - gets the cookie value - automatically runs if you call jsCookie.exists()
jsCookie.exists() - check to see if a cookie exists
jsCookie.destroy() - expire the cookie immediately


####################################
*/



// Setting a Cookie
var mycookie=new jsCookie();
mycookie.path="/";
mycookie.domain="";
mycookie.secure=false;
mycookie.name="testcookie";
mycookie.value="is chocolate chip" <br/>
mycookie.expires="0,0,0,1"; // Minutes, Hours, Days, Years
mycookie.set();



// Getting a Cookie
var mycookie=new jsCookie();
mycookie.name="testcookie"

if (mycookie.exists()){
alert(mycookie.value);
}


/*
// Destroying a Cookie
var mycookie=new jsCookie();
mycookie.name="testcookie";

if(mycookie.exists()){
mycookie.destroy();
}
*/

/*
// Iterating over multiple cookies to find a specific one

<i> </i> var mycookie=new jsCookie();
<i> </i> mycookie.get();

<i> </i> for (i=0;i&lt;mycookie.cookiearray.length;i++){
<i> </i> alert(mycookie.cookiearray[i].name+"="+mycookie.cookiearray[i].value);
<i> </i> }

*/


&lt;/script&gt;
Copy linkTweet thisAlerts:
@JavaHead_JonnieauthorFeb 13.2004 — and that sets the cookie for the name? wouldn't i have to add an onclick event etc.?
Copy linkTweet thisAlerts:
@crh3675Feb 15.2004 — The script I provided allows cooke manipulation in an easy fashion. Yes, you would need some sort of onlick event to trigger it. So, you would have
<i>
</i>
&lt;script&gt;
function SetCookie(item,value){
// Setting a Cookie
var mycookie=new jsCookie();
mycookie.path="/";
mycookie.domain="";
mycookie.secure=false;
mycookie.name=item;
mycookie.value=value; <br/>
mycookie.expires="0,0,0,1"; // Minutes, Hours, Days, Years
mycookie.set();
}
&lt;/script&gt;

&lt;a href="#" onclick="SetCookie('this','that');return false"&gt;

Copy linkTweet thisAlerts:
@JavaHead_JonnieauthorFeb 16.2004 — thanks, i thought so. I've never used cookies before, so perhaps you could explain the second script for me please...
×

Success!

Help @JavaHead_Jonnie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...