Rushy Posted October 24, 2007 Share Posted October 24, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/74566-php-cookie-help/ Share on other sites More sharing options...
GingerRobot Posted October 24, 2007 Share Posted October 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/74566-php-cookie-help/#findComment-376857 Share on other sites More sharing options...
Rushy Posted October 24, 2007 Author Share Posted October 24, 2007 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 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? Because PHP wouldn't even parse it. Quote Link to comment https://forums.phpfreaks.com/topic/74566-php-cookie-help/#findComment-376863 Share on other sites More sharing options...
GingerRobot Posted October 24, 2007 Share Posted October 24, 2007 Because PHP wouldn't even parse it. So you get an error? What is the error? Quote Link to comment https://forums.phpfreaks.com/topic/74566-php-cookie-help/#findComment-376866 Share on other sites More sharing options...
Rushy Posted October 24, 2007 Author Share Posted October 24, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/74566-php-cookie-help/#findComment-376868 Share on other sites More sharing options...
GingerRobot Posted October 24, 2007 Share Posted October 24, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/74566-php-cookie-help/#findComment-376869 Share on other sites More sharing options...
Rushy Posted October 24, 2007 Author Share Posted October 24, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/74566-php-cookie-help/#findComment-376871 Share on other sites More sharing options...
GingerRobot Posted October 24, 2007 Share Posted October 24, 2007 I assume you mean the value? Im not quite sure why you would want to, but you have two options. You could either set a different cookie variable for each of your checkboxes, or you could serialize the array and store the serialized array as a single variable in your cookie. Quote Link to comment https://forums.phpfreaks.com/topic/74566-php-cookie-help/#findComment-376985 Share on other sites More sharing options...
jeeves245 Posted October 24, 2007 Share Posted October 24, 2007 I want to do this because when a user selects a product from the catalog, its name needs to be stored in a cookie. How would I set a different cookie variable for each checkbox? Quote Link to comment https://forums.phpfreaks.com/topic/74566-php-cookie-help/#findComment-377286 Share on other sites More sharing options...
only one Posted October 24, 2007 Share Posted October 24, 2007 if(isset($_POST['checkbox1'])) { setcookie("checkbox1", true) } if(isset($_POST['checkbox2'])) { setcookie("checkbox2", true) } etc. Quote Link to comment https://forums.phpfreaks.com/topic/74566-php-cookie-help/#findComment-377307 Share on other sites More sharing options...
jeeves245 Posted October 24, 2007 Share Posted October 24, 2007 Where abouts in my code would I put that? It's just being parsed as text on the top of my page.. Quote Link to comment https://forums.phpfreaks.com/topic/74566-php-cookie-help/#findComment-377311 Share on other sites More sharing options...
jeeves245 Posted October 24, 2007 Share Posted October 24, 2007 edit Quote Link to comment https://forums.phpfreaks.com/topic/74566-php-cookie-help/#findComment-377343 Share on other sites More sharing options...
Rushy Posted October 24, 2007 Author Share Posted October 24, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/74566-php-cookie-help/#findComment-377345 Share on other sites More sharing options...
Rushy Posted October 25, 2007 Author Share Posted October 25, 2007 Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/74566-php-cookie-help/#findComment-377630 Share on other sites More sharing options...
Rushy Posted October 26, 2007 Author Share Posted October 26, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/74566-php-cookie-help/#findComment-378923 Share on other sites More sharing options...
atlanta Posted October 26, 2007 Share Posted October 26, 2007 you could set the cookie like so.. setcookie("TestCookie", $_POST['thenameofformitem']); or $_COOKIE['cookiename'] = $_POST['thenameofformitem']; i believe you can do that. Quote Link to comment https://forums.phpfreaks.com/topic/74566-php-cookie-help/#findComment-378927 Share on other sites More sharing options...
Rushy Posted October 26, 2007 Author Share Posted October 26, 2007 Yeah that's what was suggested above. Then what do I define the form value to? I tried "value=$_POST" but the cookie didn't set Quote Link to comment https://forums.phpfreaks.com/topic/74566-php-cookie-help/#findComment-378954 Share on other sites More sharing options...
Rushy Posted October 27, 2007 Author Share Posted October 27, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/74566-php-cookie-help/#findComment-379115 Share on other sites More sharing options...
PHP_PhREEEk Posted October 27, 2007 Share Posted October 27, 2007 Add semi-colons at the end of each setcookie call... PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/74566-php-cookie-help/#findComment-379189 Share on other sites More sharing options...
Rushy Posted October 27, 2007 Author Share Posted October 27, 2007 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/74566-php-cookie-help/#findComment-379197 Share on other sites More sharing options...
Rushy Posted October 28, 2007 Author Share Posted October 28, 2007 Am I allowed to bump this? Quote Link to comment https://forums.phpfreaks.com/topic/74566-php-cookie-help/#findComment-380078 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.