Forum Moderators: coopster

Message Too Old, No Replies

Help with cookies please

         

ksponline

6:19 am on Jan 24, 2004 (gmt 0)

10+ Year Member



Hi,

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>&nbsp;</td>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td width="368"><input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Reset"></td>
<td width="94">&nbsp;</td>
</tr>
</table>

</form>
</body>
</html>


---------------

Thanks,
Kathy

Birdman

1:34 pm on Jan 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Kathy,

I think you're on the right track. It looks like you just need to alter your PHP code to use the POSTed data from the form.

Change this line:

$value = 'something from somewhere';

To:

$value = $_POST["Name"];

That should get your 'Name' value into the cookie.

ksponline

5:05 pm on Jan 24, 2004 (gmt 0)

10+ Year Member



Hi, and thank you for replying. One of the many things I had tried before posting was simply $value = 'Name'; so I see now how I should have done that.

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>&nbsp;</td>
<td width="368"><input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Reset"></td>
<td width="94">&nbsp;</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

ksponline

3:46 am on Jan 26, 2004 (gmt 0)

10+ Year Member



Can't anyone help with this? I'm not asking for the code, just a clue so I know what to focus on. Please...

BitBanger

5:17 am on Jan 26, 2004 (gmt 0)

10+ Year Member



In proc_cookie.php, I see where you are mailing the data, but where are you setting the cookie?

Also, you need to find a way to place multiple variables into a single cookie. Look at serialize() and unserialize() functions for one possible solution.

ksponline

3:36 am on Jan 27, 2004 (gmt 0)

10+ Year Member



I'm not familiar with serialize() and unserialize(), so I will look those up in the php manual.

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>&nbsp;</td>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td width="368"><input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Reset"></td>
<td width="94">&nbsp;</td>
</tr>
</table>

</form>
</body>
</html>

Thank you...

BitBanger

12:06 pm on Jan 27, 2004 (gmt 0)

10+ Year Member



In form 1, the '$value' variable is given a value of 'Name' and is put into the cookie. If you want to pick that info up in page two, then your form input statement should look like this:

<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.

ksponline

3:49 am on Jan 28, 2004 (gmt 0)

10+ Year Member



Thank you, thank you, thank you!

That's working now and makes sense.

Now I'll move on to the concept of serialize() and unserialize()! Thanks again for your help and for pointing me in the right direction.

Take care,
ksp