Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/171969-solved-cookies-submitting-a-vlaue/
Share on other sites

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.

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.

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>

 

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.