...

View Full Version : Resolved How do i make my website remember user input.



MancunianMacca
03-02-2012, 05:18 PM
Ok, so if you navigate ovr to my website "http://www.mancunianmacca.co.cc" you will notice that a box pops up asking for your name. When you type it in it adds it to the page. Is there a way I can get this to remember the users input and only ask them once then remember. Like cookie or anything. If you need any extra things. Just ask.

Philip M
03-02-2012, 05:43 PM
Yes, you need to use a cooke (althugh users may disable or erase them). Cookies have been dealt with very many times in this forum - have you tried using the search feature?

Here is what you want:-


<html>
<head>

<script type="text/javascript">
function changeText2(){
var userInput = document.getElementById('uInput').value;
userInput = userInput.replace(/[^a-z\-\s\']/gi,""); // letters space hyphen apostrophe only
userInput = userInput.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()});
userInput = userInput + "!"; // add a !
document.getElementById('boldStuff2').innerHTML = userInput;
document.getElementById("stuff").style.display="none";
createCookie ("UserName",userInput,365);
}

function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else {var expires = ""}
document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function eraseCookie(name) {
createCookie(name,"",-1);
}
</script>
</head>

<body>
<p>Welcome to the site <b id='boldStuff2'></b> </p>
<div id = "stuff" style="display:block">
Enter your name here <input type='text' id='uInput' onchange = "changeText2()">
</div>

<script type="text/javascript">
// this script must come after the divs
if(readCookie('UserName')) {
var n = readCookie('UserName');
document.getElementById("boldStuff2").innerHTML = n;
document.getElementById("stuff").style.display="none";
}

</script>

</body>
</html>Quizmaster: What is the easternmost destination on the Trans-Siberian Railway?
Contestant: Vienna.

MancunianMacca
03-02-2012, 05:45 PM
Nope but I will now, thanks

MancunianMacca
03-02-2012, 05:50 PM
Yes, you need to use a cooke (althugh users may disable or erase them). Cookies have been dealt with very many times in this forum - have you tried using the search feature?

Quizmaster: What is the easternmost destination on the Trans-Siberian Railway?
Contestant: Vienna.

Just done a search and I can't find anything that relates to what I need. I haven't used cookies before. I was trying to figure this out myself online and founf somthing like "session storage" would that be a better way than cookies. Anywho please help!

Philip M
03-02-2012, 06:38 PM
I gave you the code you require in Post#2. "Session Storage" is only implemented in HTML5 (the most modern browsers). As its name implies, session storage only persists for that one session. You probably mean "Local Storage" - and the same applies. Cookies are perfectly adequate for what you want - just to to remember a name.

MancunianMacca
03-02-2012, 07:08 PM
I gave you the code you require in Post#2. "Session Storage" is only implemented in HTML5 (the most modern browsers). As its name implies, session storage only persists for that one session. You probably mean "Local Storage" - and the same applies. Cookies are perfectly adequate for what you want - just to to remember a name.

Ok so i've got this done and evrything works great exept from the write in bit. Here is the code i have in an extrnal .js file



function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}


and to make it write in i'm trying to use this code.



<script>
function checkCookie()
{
var username=getCookie("username");
var addtext = username;
if (username!=null && username!="")
{
document.getElementById("addtext").innerHTML = addtext;
}
else
{
username=prompt("Please enter your name:","");
if (username!=null && username!="")
{
setCookie("username",username,365);
}
}
}
</script>

Now the prompt works so the .js file is working, but it will not write, I had it wirting when i used "writeln" but it wrote to a blank page not to the page.

Please help, Thanks.

MancunianMacca
03-02-2012, 10:40 PM
I gave you the code you require in Post#2. "Session Storage" is only implemented in HTML5 (the most modern browsers). As its name implies, session storage only persists for that one session. You probably mean "Local Storage" - and the same applies. Cookies are perfectly adequate for what you want - just to to remember a name.

I used your code and it works great... But, I want it to be a Prompt not a text field. I tried to adapt on your code to try this but no luck. Please help...
Code I tried...


<body>
<p>Welcome to the site <b id='boldStuff2'></b> </p>
<div id = "stuff" style="display:block">
<script type="text/javascript">
changeText2=prompt("Please enter your name:","");
</script>

</div>

Old Pedant
03-02-2012, 11:58 PM
<script>
function checkCookie()
{
var username=getCookie("username");
if (username ==null || username =="")
username=prompt("Please enter your name:","");
setCookie("username",username,365);
}
document.getElementById("addtext").innerHTML = username
}
}
</script>

LOGIC. You need to learn to use simple logic. Nothing is going to *AUTOMATICALLY* put the prompted-for name into the innerHTML. So you need to do that NO MATTER if the username comes from a cookie or from the prompt.

Philip M
03-03-2012, 07:57 AM
Prompt is a very crude and amateurish method of obtaining user data. But surely you can make that simple alteration yourself? I get the impression that you are trying to run before you can walk - or even crawl. changeText2 is the name of a function, not a variable. Do you understand the difference?

Old Pedant:-

A realist drinks it no matter how much there is.
Does that include the poison in the jewelled glass you used to have in your sig? :D

Personally I believe the glass is superfluous - you get the same result quicker and cheaper by drinking straight from the bottle. :D

Lady Astor said to Winston Churchill "If I was married to you, I would put poison in your whisky".
To which Churchill replied "If I was married to you, I would drink it".

MancunianMacca
03-03-2012, 01:03 PM
Prompt is a very crude and amateurish method of obtaining user data. But surely you can make that simple alteration yourself? I get the impression that you are trying to run before you can walk - or even crawl. changeText2 is the name of a function, not a variable. Do you understand the difference?

Old Pedant:-

A realist drinks it no matter how much there is.
Does that include the poison in the jewelled glass you used to have in your sig? :D

Personally I believe the glass is superfluous - you get the same result quicker and cheaper by drinking straight from the bottle. :D

Lady Astor said to Winston Churchill "If I was married to you, I would put poison in your whisky".
To which Churchill replied "If I was married to you, I would drink it".

I think you're right, but i'm finding it so hard to learn It's just like i'm not learning at all just copying others code. I am really trying to learn and I know that I am proberbly really annoying. So I couldn't get the prompt to work so I used your original text box code for now. I'm going to keep trying but at the lower level stuff.

Philip M
03-03-2012, 03:33 PM
I think you're right, but i'm finding it so hard to learn It's just like i'm not learning at all just copying others code. I am really trying to learn and I know that I am proberbly really annoying. So I couldn't get the prompt to work so I used your original text box code for now. I'm going to keep trying but at the lower level stuff.

That is precisely the impression that I get. You must master the basics first. It is like learning a spoken langauge - the first steps are difficult, but it does get easier. But if you do not master the fundamentals you will never get anywhere.

It was as easy as this:-

var userInput = prompt("Please enter your name", "");



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum