snk Posted December 9, 2007 Share Posted December 9, 2007 Hello, I am going to make a quiz game solely with cookies. My problem is that I dont know how to set the cookie's variables on the submit procedure. I need to set the user's enter of a textbox (answer) and to transfer a value (score). I set the cookie at the beginning as the php manual says. If I could use POST or GET I know how I was going to make it, but now there is restriction with only cookies and not post/get. Is there any Javascript piece of code that makes the magic? If yes could you give me an example or a link to? thank you in advance. Quote Link to comment Share on other sites More sharing options...
Foser Posted December 9, 2007 Share Posted December 9, 2007 I believe sessions would do a better job, so that if your user wants to take it twice he will be able to. You should learn sessions more so than cookies as sessions is alot safer to store information. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 9, 2007 Share Posted December 9, 2007 You use setcookie to set it and use $_COOKIE to retrieve the data. Quote Link to comment Share on other sites More sharing options...
snk Posted December 9, 2007 Author Share Posted December 9, 2007 thanks for you replies. I have to do it with cookies, it is restriction. I know how to set and retreieve the cookie. the problem is that I need to set it with the text's value, that means user has to enter a word and then the cookie sets the value. any other suggestion? Thanks again Quote Link to comment Share on other sites More sharing options...
revraz Posted December 9, 2007 Share Posted December 9, 2007 Not sure what the problem is <?php $word = "hi"; setcookie ('word', $word, time () + (60*60*24*365),'/'); ?> Quote Link to comment Share on other sites More sharing options...
snk Posted December 9, 2007 Author Share Posted December 9, 2007 thanks for helping me, the problem is... that I dont set the cookie when i submit the form. To understand what i want to do is to make a quiz. a user enters the answer, presses submit button and the next page shows if the answer is correct or not. thanks again for your time. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 9, 2007 Share Posted December 9, 2007 I still don't see the problem. When do you want to set the cookie? Quote Link to comment Share on other sites More sharing options...
snk Posted December 9, 2007 Author Share Posted December 9, 2007 If i set the cookie before somebody submits the answer.... I will have a cookie with my value not with visitor's answer. I want to pass the visitor's answer, the cookie has to set up dynamicaly. So far you tell me to set a cookie before a visitor's answer. I hope you see it now and give me a hand Quote Link to comment Share on other sites More sharing options...
revraz Posted December 9, 2007 Share Posted December 9, 2007 I didn't state either way. Why can't $word be their answer? So far you tell me to set a cookie before a visitor's answer. Quote Link to comment Share on other sites More sharing options...
snk Posted December 9, 2007 Author Share Posted December 9, 2007 I know that sounds stupid... I know that I should send it via POST GET variables... But it is Coursework, so dont ask why... Anyway I found the solution, you need javascript to do that. I will post later details, it is quite interesting. Quote Link to comment Share on other sites More sharing options...
LuckY07 Posted December 9, 2007 Share Posted December 9, 2007 here's a link that might help: http://www.spoono.com/php/tutorials/tutorial.php?id=18 Quote Link to comment Share on other sites More sharing options...
revraz Posted December 9, 2007 Share Posted December 9, 2007 That tutorial isnt exactly accurate, this is not a true statement You have to put the cookie code as the first line of your file, it has to be before the head, before any other code, HTML or PHP. You can have PHP before the cookie and it will still work. I have a login page that sets a cookie only if they choose to stay logged in. Quote Link to comment Share on other sites More sharing options...
snk Posted December 9, 2007 Author Share Posted December 9, 2007 a cookie has to set up before any output. You can do it in the body tag as well as far as there is no any code that produces output to the page. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 9, 2007 Share Posted December 9, 2007 Yep, which is why I said that site was wrong. Quote Link to comment 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.