March 14, 200818 yr Hi, I think I can search the web for a tutorial explaining cookies, but the idea is usage. I am running a free to use guestbook (meaning you type a name and a message and post it) and the name section is a text line. The text line has an initial value and I want the cookie to containt the last typed name by the user so that it can be used as an initial value instead of the default. The bad thing is that I don't know how to use cookies in html or anyhow for that matter. Thanks for any help.
March 14, 200818 yr You can't use cookies with HTML on it's own. You will need to use a server-side language (like PHP) for it to work for everyone, otherwise you could use Javascript, but then you have the possibility of them turning off JS and it not working. I would recommend a server-side method
March 14, 200818 yr Author Ok I actually got that, BUT I'm not sure how to realise it. Lets assume I have made the cookie. I don't know how to access it (the PHP site doesn't really offer a good example and I need one), store it in a variable and force it onto a text field. Let me specify what I want: a function that stores the cookie's value to a variable ($str - I might be able to do that) and I want <input name="name" type="text" id="name" value="something" size="20"> to use the $str variable instead of "something" I'm guessing the first is easy, but the third one is a b**ch. Ofcourse this is just what I think is the easyest way to make this hapen, but I'm open to suggestions.
March 14, 200818 yr You can use $_COOKIE . If you set the cookie name like $name = 'myCookie'; $myCookieValue = $_POST['inputnamewithvalueIwant']; setcookie($myCookie", $myCookieValue, time()+3600); Then to access the cookie you'd use echo $_COOKIE[$name']; eg <input name="name" type="text" value="<?=$_COOKIE[$name];?>" /> I do not recommend using any of this example in an actual live site. Lol.
March 14, 200818 yr It doesn't sound like you need a function for this. All you need to do is store the (cleaned up) cookie values into some variables (eg. $var1, var2), and then use those in the value="" attribute of the input field. For example: <?php // The following 2 lines will set the cookies setcookie('name', 'Brian'); // You can add extra arguments to set a date for the cookie to expire, etc. setcookie('email', 'brian@fakemailaddress.com'); // You can add extra arguments to set a date for the cookie to expire, etc. // The following 2 lines will retrieve the data inside the cookies, and then set them to variables. $cookie_name = $_COOKIE['name']; $cookie_email = $_COOKIE['email']; ?> <input type="text" name="guestbook_name" value="<?php echo $cookie_name; ?>"> <input type="text" name="guestbook_email" value="<?php echo $cookie_email; ?>"> That is a very basic example, but you should be able to get the rough idea. Rob
March 14, 200818 yr Author IT WORKS!!! One thing though. How can I make the cookie secure enough so that it is accepted at highers levels of privacy? (other than low I mean)
Create an account or sign in to comment