0

TLDR:- What is a good way to pass contents of a variable from one PHP file to another without involving a form, link or a button.

Question:-

So there is a form in a page/file called question_edit_form.php and its action attribute is already set to another file called question.php. The variable of interest is being read-in from the user in question_edit_form.php and is then obviously being sent to question.php using $_POST.

Now, there is a third file, named renderer.php, and which is not linked to the other two files. I want to use that variable of interest in this file. So how can I access that variable which is set in question.php from inside renderer.php?

iankit
  • 8,806
  • 10
  • 50
  • 56
Solace
  • 8,612
  • 22
  • 95
  • 183

4 Answers4

2

first file -

session_start();
$_SESSION['your_variable'] = 'value';

other file -

session_start();
$var = $_SESSION['your_variable'];

this may help.

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
1

Generally there are two methods available for you to pass on the value

  1. Cookies

  2. Sessions

How to use cookies:-

setcookie(name, value, expire);

e.g.

setcookie("user", "Alex Porter", time()+3600);

Access it using echo $_COOKIE['user'];

Second is Sessions. Here is how to use sessions:-

 session_start();
 $_SESSION['varname']=value;

Accessing page:-

session_start();
echo $_SESSION['varname'];

Additional info if required:- Make sure you use session_start() at top of your page if not you may face with an headers already sent error / warning which again can be fixed by output buffering ob_start()

Solace
  • 8,612
  • 22
  • 95
  • 183
NaveenThally
  • 946
  • 1
  • 6
  • 18
1

It sounds like you are using Moodle, in which case renderer.php is not an independent file; it contains the class definition for the renderer object used by question.php.

So... there is no need to pass the parameter between the scripts. If you really must access the form value directly from the renderer, just use the standard methods from the Moodle framework: required_param($name, $type) or optional_param($name, $default, $type).

Chris Throup
  • 651
  • 1
  • 4
  • 19
  • That's right I am using Moodle. Thanks for the response, but I actually tried to access it from the renderer by using `required_param('qtype', PARAM_TEXT)`, and also from `attempt.php` (which calls `attempt_page()` function of renderer) but i keep getting `A required parameter $qtype is missing`. The network monitor in Firefox also shows that it is not being received by `renderer.php` or `attempt.php`, while it does get sent from the original form. – Solace Aug 01 '14 at 13:31
  • The worse part is that the session variables are not working for me either. – Solace Aug 01 '14 at 13:33
  • 1
    @Zarah How are you using `renderer.php`? It's usually included within a script (via `$PAGE->get_renderer()`), not accessed directly from the browser. – Chris Throup Aug 01 '14 at 13:36
  • Yes, the renderer of interest is the renderer of the `Quiz` module. In attempt.php of quiz module, we get the renderer instance by `$PAGE->get_renderer('mod_quiz');` and then use that renderer instance to call the method `attempt_page()`... Now, the variable I need to use in this `attempt_page()` method is read in from the user in the `edit_question_form.php` (in the `Question plugin`); the `action` of this form is set to `question.php`, so the variable is being sent to `question.php`. Now I need to access this variable from `question.php` from the Quiz renderer. – Solace Aug 01 '14 at 14:05
0

You can store the variables in the session.

http://www.w3schools.com/php/php_sessions.asp

Sunil Khiatani
  • 337
  • 4
  • 12