0

This is a follow-up question to this one: How to access a variable from one PHP page to another, without a form, link, or button?

SCENERIO:-

As mentioned in the first question, there is a form defined in a file named question_edit_form.php. The action of this form is set to a file named question.php. So all the data in the form is obviously passed to question.php.

But I added a select element in the form and I want this element's data to be passed to another file named renderer.php. This file is responsible for displaying the question to the student in a quiz so that they can write its answer.

FIRST PROPOSED SOLUTION- SESSION VARIABLE:-

So the first solution was to use a session variable. It worked in one scenerio, but failed in the other, 'practical' one.

The thing is the question_edit_form is used by a teacher to edit a question- that is one session. A student answers that question in another session. So obviously that session variable is not available to renderer.php when it displays the question to the user (because it is another session).

SECOND PROPOSED SOLUTION - COOKIES:-

The other solution is using cookies. But reading up on cookies here tells me that

A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too.

So the server will put the variable on the user's computer. But the student will attempt many questions in the same session and the variable's value will be different for each question.

QUESTION:-

So can cookies be used in this scenerio? or is there another solution to pass the variable to the file renderer.php?

Community
  • 1
  • 1
Solace
  • 8,612
  • 22
  • 95
  • 183
  • 1
    How about to put it in URL? – zerkms Aug 04 '14 at 02:28
  • @zerkms Thank you. Shall it be available to `renderer.php` which is not directly linked from the `question_edit_form.php`? – Solace Aug 04 '14 at 02:53
  • 1
    I'm not sure what you're talking about. But URL and all its data is available to a script that handles it. – zerkms Aug 04 '14 at 02:54
  • 1
    To expand on the URL use a get request to pass your information from one page to another. Note, it's also client sided redirect which means user can modify the request. If it's sensitive, use a session – Daryl Gill Aug 04 '14 at 03:06
  • @zerkms The thing is that the `action` attribute of the form is set to a file named question.php (It's URL is something like `localhost/moodle/question/question.php`). The file I want to access it from is another file (with the URL `localhost/moodle/mod/quiz/attempt.php`). So can I add the variable to the URL of attempt.php file, in/from a file which is not linked to `attempt.php` in any way? – Solace Aug 04 '14 at 03:10
  • @DarylGill I can not use session variables because the form will be used in one session (by the teacher), and the file I want to access the variable from will be used in another session, by another user (that is the student). – Solace Aug 04 '14 at 03:21
  • @DarylGill To the best of my understanding, both POST and GET methods send the variable to the page which is in the form's `action` attribute. I need to access the variable from a third file, which is not the form's action. – Solace Aug 04 '14 at 05:25

1 Answers1

0

You can use cookies for this requirement - it's possible either to set a different cookie for each question or to store all questions/answers as value of a single cookie. Not knowing the full request just an example: in case the quiz is a multiple choice where the value to be stored as answer is just a number or letter, the cookie value can just start from 1=a (for question 1, answer a), 1=a&2=c and so on. In addition it's possible to parse the cookie value to check if an answer was already given, in case it's allowed to change the first answer, and to adjust the cookie value accordingly. For simplicity just an example for the case that every question would only allowed to be answered once.

First, check if the cookie already exists. E.g. using a cookie named _quizCookie:

function checkQuizCookie() {
  var i,x,y,ARRcookies = document.cookie.split(";");
  for (i = 0; i < ARRcookies.length; i++) {
    x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
    y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
    x = x.replace(/^\s+|\s+$/g, "");
    if (x.indexOf("_quizCookie") != -1) {
        return false;
    }
   }
 }

Calling prepareQuizCookie() when the page loaded:

function prepareQuizCookie() {
  if (false != checkQuizCookie()) {
     setQuizCookie();
  }
}

So in case _quizCookie already exists, it won't be overwritten; in case it's not there, it will be created:

function setQuizCookie() {
  var now = new Date(),
      expireDate = new Date();
  expireDate.setTime(now.getTime() + (10 * 365 * 24 * 60 * 60 * 1000));
  document.cookie = "_quizCookie=start" + "; expires=" + expireDate.toUTCString() + "; path=/";
}

When question 1 would be answered with answer a, the value of the cookie can be adjusted calling a function that retrieves the actual cookie value and appends a key/value-pair for the current answer separated by "&":

function changeQuizCookie(answer) {
  var now = new Date(),
      expireDate = new Date(),
      quizCookieVal = getQuizCookieValue(),
      newQuizCookieVal = quizCookieVal + "&" + answer;
  expireDate.setTime(now.getTime() + (10 * 365 * 24 * 60 * 60 * 1000));
  document.cookie = "_quizCookie=" + newQuizCookieVal + "; expires=" +   expireDate.toUTCString() + "; path=/";
}

So for the example when question 1 would be answered with a, call changeQuizCookie("1=a"). The cookie value would then be start&1=a. I just set "start" as initial cookie value which shouldn't be necessary. I haven't checked if the suggested approach is working (but think it should), but it's possible just to set the cookie without any value when it's set initally and, when it's value is changed, to check if quizCookieVal is empty; if it's empty, newQuizCookieVal = answer; if not, newQuizCookieVal = quizCookieVal + "&" + answer.

And to get the current cookie value:

function getQuizCookieValue() {
  var name = "_quizCookie=",
      ca = document.cookie.split(';');
  for(var i=0; i<ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1);
    if (c.indexOf(name) != -1) {
        return c.substring(name.length, c.length);
    }
  }
  return "";
}

This is all just as example for how to handle the required functionality using a cookie, as you asked if cookies could be used mentioning that the variable would change for each question. So this shouldn't be a problem by just retrieving the current cookie value and overwriting it by keeping the previous cookie value and just appending the current question/answer key/value. Other approach would be to set a single cookie for each question/answer, but just wanted to give an example for a single-cookie approach.

matthias_h
  • 11,356
  • 9
  • 22
  • 40