View Full Version : value from cookie

05-09-2009, 01:31 AM
How to submit content from html form to cookie and then read it from there as form value?

05-09-2009, 02:27 AM
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="cookie" />
<input type="submit" value="Submit" />
//set the cookie value to the data from the form
if(isset($_POST['submit'])) {
setcookie("cookie_name", $_POST['cookie'], time()+3600);

//retrieve the data from cookie and show it
echo $_COOKIE['cookie_name'];

That should do it.
For more info on cookies, check out http://www.w3schools.com/PHP/php_cookies.asp

05-09-2009, 08:55 AM
thang you, but it does not create any cookie to read from.
objective is to replace form value each time with something submited from there.

05-10-2009, 10:00 PM

$form = $_POST['form'];

if($_GET['cookie'] == 'new'){
setCookie('cookie', 2 );
header('Location: '.$_SERVER['PHP_SELF']);
}elseif($_GET['cookie'] == 'del'){
header('Location: '.$_SERVER['PHP_SELF']);
if($_COOKIE['cookie'] != null){
echo 'Cookie exists!<br>
<a href="'.$_SERVER['PHP_SELF'].'?cookie=del">Delete Cookie</a><br>';

echo 'No cookie!<br>

<a href="'.$_SERVER['PHP_SELF'].'?cookie=new">New Cookie</a><br>';

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post"><input name="form" value="<?php if(isset($_COOKIE['cookie'])){ echo $_COOKIE['cookie']; } ?>" /><input name="form" type="hidden" value="">
<a href="<?php $_SERVER['PHP_SELF'] ?>?cookie=new">New or Chance</a></form>

now how can i submit both form and .$_SERVER['PHP_SELF'].'?cookie=new at the same time ?

05-12-2009, 12:09 PM
any ideas ?