Jump to content

PHP Cookie Help


Rushy

Recommended Posts

Hey all,

 

i'm learning PHP at the moment and i'm having a bit of trouble with cookies. I know how to create a single cookie with a set name, but how do I create a cookie that takes its name/value from a form? The form itself is in a loop that spits out info from a database, and the form name also is taken from the database as it loops through.

 

I was trying to use the Post method but it didn't work so obviously this isn't the way to do it.

 

Could anyone point me in the right direction? :)

 

 

Link to comment
Share on other sites

How about showing us the code you have? There is no reason why you wouldn't be able to set a cookie from post data.

 

Show us what you've done so far, and tell us what happens when you try that code.

 

Ok, this is the loop:

    for ($i = 0; $i < $nrows; $i++ )
    {
      echo "<form method=post><tr>\n"; 
      echo "<td><input type=checkbox name=products value=" . $results["TITLE"][$i] . " | $" . $results["PRICE"][$i] . "> <font color=white> " . $results["TITLE"][$i] . "</td>";
    **Rest of code removed**
    }
  ?>

 

So as you can see, the form name is taken from the DB.

 

Now please don't laugh if this code is absolute crap, i've been using PHP for a few days :P But this is what I tried:

 

<?php
    setCookie('Products', $_POST, time()+3600);
?>

 

And then I tried making "value=$POST" in the form tags.

 

I'm guessing this is completely wrong? :P Because PHP wouldn't even parse it.

Link to comment
Share on other sites

Because PHP wouldn't even parse it.

 

So you get an error? What is the error?

 

Oops, sorry.

 

Actually I just tried, and it did parse it. Warning: setcookie() expects parameter 2 to be string, array given in C:\Web\WebServer\Apache2\htdocs\products.php on line 2

Link to comment
Share on other sites

Well there you go then. The value of the cookie that you store must be a string, not an array. $_POST is an array containing all of the information that has been sent to the page via the post method.

 

What is it that you actually want to store?

 

I want to store the name of each checkbox.. which is taken from the DB as you can see from the code. Is there a better way of doing it?

Link to comment
Share on other sites

Where abouts in my code would I put that? It's just being parsed as text on the top of my page..

 

Oops. I didn't realise I already had an account here!

 

Anyway ignore that, I forgot to put it in <? .. ?> tags!  :-\

 

Although now the cookie isn't being set. Do I define value as value=$_POST? OR something else?

Link to comment
Share on other sites

Bump. Some help would be appreciated, i'm still a bit stuck :(

 

Oops. I didn't realise I already had an account here!

 

Anyway ignore that, I forgot to put it in <? .. ?> tags!  :-\

 

Although now the cookie isn't being set. Do I define value as value=$_POST? OR something else?

Link to comment
Share on other sites

Hmm now i'm getting this error with the following code:

 

<?php
if(isset($_POST['checkbox1']))
{
  setcookie("checkbox1", true)
{
  
if(isset($_POST['checkbox2']))
{
  setcookie("checkbox2", true)
}
?>

 

 

Parse error: syntax error, unexpected '{' in C:\Web\WebServer\Apache2\htdocs\products.php on line 5

Link to comment
Share on other sites

Haha oops I knew that!

 

Although now I get "Parse error: syntax error, unexpected $end in C:\Web\WebServer\Apache2\htdocs\products.php on line 56" which is the last line of the program (blank).

 

Also, do I define the form value as value=$_POST  ?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.