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?
You can store the variables in the session.
http://www.w3schools.com/php/php_sessions.asp
Generally there are two methods available for you to pass on the value
Cookies
Sessions
How to use cookies:-
e.g.
Access it using
echo $_COOKIE['user'];
Second is Sessions. Here is how to use sessions:-
Accessing page:-
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()
first file -
other file -
this may help.
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 byquestion.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)
oroptional_param($name, $default, $type)
.