Forum Moderators: coopster
We have a form that's divided into two parts. Someone can fill out Part I or Part II, or Parts I and II. Each form has the same personal info that needs to be filled out (name, email address, etc.).
If someone submits form 1 then also fills out form 2, I'd like them to not have to fill all their personal information out again, so I am trying to store info in cookies so that if Part I is filled out and they go directly to Part II, then the information will already be entered into the form fields on Part II.
No matter what I did, it wasn't working so I've started out small. I tried what I found on the php site:
<?php
$value = 'something from somewhere';
setcookie('Testcookie',$value);
echo $_COOKIE['Testcookie'];
?>
That prints out 'something from somewhere.'
If I put a name in the Name form field and then refresh, the name disappears althogher.
Can someone tell me what I'm doing wrong and/or give me pointers on the steps I need to take to get data form passed into the fields from one page to another (i.e., do I need to see if $Name exists, etc.?
Here's the little test page I've been using:
<?php
$value = 'Name';
setcookie('Testcookie',$value);
echo $_COOKIE['Testcookie'];
?><html>
<head>
<title>Cookie 1 Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form action="proc_cookie.php" method="post" name="cookie1">
<table width="600" border="0" cellspacing="0" cellpadding="6">
<tr>
<td colspan="3"><font size="2" face="Arial, Helvetica, sans-serif"><strong>Cookie Test</strong></font></td>
</tr>
<tr>
<td width="102"><div align="right"><font size="2" face="Arial, Helvetica, sans-serif">Name</font></div></td>
<td colspan="2"><input name="Name" type="text" id="Name" size="60"></td>
</tr>
<tr>
<td> </td>
<td colspan="2"> </td>
</tr>
<tr>
<td> </td>
<td width="368"><input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Reset"></td>
<td width="94"> </td>
</tr>
</table>
</form>
</body>
</html>
Thanks,
Kathy
I still don't pick up the cookie value on the form 2 test page though, so it must be the way I've coded that page, or the page that processes form 1 and form 2.
I've corrected the $value as you showed me. I submit the form 1 page and it's processed by proc_cookie.php, which is this:
<?php
$PageTitle = "Cookie Test";
$Name = stripslashes(trim($Name));
$message = '';
foreach($HTTP_POST_VARS as $key => $value)
{
if ($value!="")
$message .= $key . ": " . stripslashes(trim($value)) . "\n";
}
mail("me@isp.net","Cookie Test","$message","From: $Name <$email>");
header ("Location: /test/thanks.php");?>
The thanks page contains links to both form 1 and form 2. It is set up like this:
<html>
<head>
<title>Thanks</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head><body>
<font size="2" face="Arial, Helvetica, sans-serif">Go to <a href="cookie1php.php">form1</a> or <a href="cookie2php.php">form2.</a></font>
</body>
</html>
The form 2 test page is set up like the form 1 test page, but I've added code to the input name field to display the cookie...so perhaps I've not done that properly either:
<html>
<head>
<title>Cookie 2 Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body><form action="proc_cookie.php" method="post" name="cookie2">
<table width="600" border="0" cellspacing="0" cellpadding="6">
<tr>
<td colspan="3"><font size="2" face="Arial, Helvetica, sans-serif"><strong>Cookie Test</strong></font></td>
</tr>
<tr>
<td width="102"><div align="right"><font size="2" face="Arial, Helvetica, sans-serif">Name</font></div></td>
<td colspan="2"><input name="<?php echo $_COOKIE['Testcookie'];?>" type="text" id="Name" size="60"></td>
</tr>
<tr>
<td> </td>
<td width="368"><input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Reset"></td>
<td width="94"> </td>
</tr>
</table>
</form>
</body>
</html>
The reason for jumping through all the hoops, so to speak, is that I'm trying to roughly emulate the way the forms are set up at work.
Am I failing to add something critical on the proc_cookie.php form? I'm under the impression that when the cookie is set from either form 1 or form 2, that it's persistent until the browser window is closed and therefore should be picked up, in this case, on form 2 when it opens in the browser.
Do I need an if conditional on the form 2 page that indicates if there is no cookie then that form field value is "Name"?
My father had a heart attack this week and is in the hospital, so my brain is a bit overwhelmed with everything at the moment. I'm trying hard to understand how this process with cookies works... it seems like it should be fairly simple, so I don't know why it's being so difficult for me to grasp its concept.
Thanks again...
Kathy
Just to clarify things, even though I set the cookie on the form 1 test page, I still need to set it on the proc_cookie.php page?
Here's the test 1 page again:
<?php
$value = 'Name';
setcookie('Testcookie',$value);
echo $_COOKIE['Testcookie'];
?> <html>
<head>
<title>Cookie 1 Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form action="proc_cookie.php" method="post" name="cookie1">
<table width="600" border="0" cellspacing="0" cellpadding="6">
<tr>
<td colspan="3"><font size="2" face="Arial, Helvetica, sans-serif"><strong>Cookie Test</strong></font></td>
</tr>
<tr>
<td width="102"><div align="right"><font size="2" face="Arial, Helvetica, sans-serif">Name</font></div></td>
<td colspan="2"><input name="Name" type="text" id="Name" size="60"></td>
</tr>
<tr>
<td> </td>
<td colspan="2"> </td>
</tr>
<tr>
<td> </td>
<td width="368"><input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Reset"></td>
<td width="94"> </td>
</tr>
</table>
</form>
</body>
</html>
Thank you...
<input name="Name" type="text" id="Name" size="60" value="<?php $_COOKIE['Testcookie']?>">
In reality though, the cookie should be set in the proc_cookie.php file. That will get the data that the user entered into the form 1 page.