dink87522 Posted August 26, 2009 Share Posted August 26, 2009 I have a script in my PHP page which checks if a question is answered correctly. This is checked when the user submits the Submit button. I want to be able to keep a running score of the number of questions correct. I place this in a variable, however how do I then get that variable to the next page? I can't use a cookie as I can't modify the http headers there with it being near the end of the page. I was thinking of using a hidden field value, however how do I then send that to the next page (the user has already clicked submit - when checking to see if they do or don't have the right answer). Thoughts or ideas? Quote Link to comment https://forums.phpfreaks.com/topic/171969-solved-cookies-submitting-a-vlaue/ Share on other sites More sharing options...
JonnoTheDev Posted August 26, 2009 Share Posted August 26, 2009 Store in a session. Your form processing code should always be prior to any screen output! i.e. session_start() // form submitted if(strlen($_POST['submit'])) { $questionNum = $_POST['questionNum']; $answer = $_POST['answer']; // store answer $_SESSION['answers'][$questionNum] = $answer; // move onto next question header("Location:questions.php?q=".($questionNum+1)); exit(); } // form html goes down here This is a pure example. You must always validate the post data. Quote Link to comment https://forums.phpfreaks.com/topic/171969-solved-cookies-submitting-a-vlaue/#findComment-906772 Share on other sites More sharing options...
dink87522 Posted August 26, 2009 Author Share Posted August 26, 2009 I get Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/geotwe/public_html/question.php:45) in /home/geotwe/public_html/question.php on line 512 I process it at the top. However I need to send data from the middle of my script. Quote Link to comment https://forums.phpfreaks.com/topic/171969-solved-cookies-submitting-a-vlaue/#findComment-906790 Share on other sites More sharing options...
dink87522 Posted August 26, 2009 Author Share Posted August 26, 2009 ??? Quote Link to comment https://forums.phpfreaks.com/topic/171969-solved-cookies-submitting-a-vlaue/#findComment-906797 Share on other sites More sharing options...
JonnoTheDev Posted August 26, 2009 Share Posted August 26, 2009 session_start(); Must be called prior to any output! HTML, echo, etc. Place at the top of all scripts that require session data. Best used in a common include file on all scripts i.e include('config.inc.php'); You should not be processing form data with your script in the middle of HTML output. It should come prior. There is no reason why this should be an issue. You need to look at re-organising your code structure as your layout sounds poor. Organisation: // common includes include('config.inc.php'); // processing scripts & functions function x() { } function y() { } if(strlen($_POST['submit'])) { $x = x(); $y = y(); } // html / php output <html> <head></head> <body> <form></form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/171969-solved-cookies-submitting-a-vlaue/#findComment-906799 Share on other sites More sharing options...
dink87522 Posted August 26, 2009 Author Share Posted August 26, 2009 Thanks for the excellent explanation, that cleared up a couple of things and it now works! Quote Link to comment https://forums.phpfreaks.com/topic/171969-solved-cookies-submitting-a-vlaue/#findComment-906809 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.