I'm having problems using the PHP GLOBALS array in a Joomla website. When the form is submitted, the function form_submit is called where the form information is checked for validity. For some reason, I can access my variables correctly outside the function, but when I try to access them through the GLOBALS array, nothing is found.
<?php
//THIS CODE CREATES THE ADD COURSE FORM
//CONNECT TO SERVER
require('../database2/includes/connect.php');
//GET LOGGED IN USER INFO
$user = JFactory::getUser();
$user_id = $user->id;
$user_name = $user->name;
$category_query = $conn->query('SELECT * FROM category');
$category_query->setFetchMode(PDO::FETCH_ASSOC);
$name = $_POST['name'];
$description = $_POST['description'];
$category_id = $_POST['dropdown'];
$crn = $_POST['crn'];
$password_init = $_POST['password_init'];
$password_rt = $_POST['password_rt'];
$password = md5($password_init);
function form_submit()
{
var_dump($GLOBALS['name']); //Dumps null
global $name //Doesn't work either
if (empty($name) || empty($description) || empty($crn) || empty($password_init) || empty($password_rt))
{
echo "<b style='color:red'>* $name</b><br>";
echo "<b style='color:red'>* $description</b><br>";
echo "<b style='color:red'>* $crn</b><br>";
echo "<b style='color:red'>* $password_init</b><br>";
echo "<b style='color:red'>* $password_rt</b><br>";
}
}
if(isset($_POST['Submit']))
{
var_dump($name); //Dumps correct value
form_submit();
}
?>
var_dump($name) prints the correct value, but $GLOBALS['name'] inside the form_submit does not. What is wrong with my code?
Given your mention of Joomla, and the code's mention of a class JFactory
which must be defined elsewhere, I suspect that this file is not the direct entry point of the browser, but is included by the framework.
The reason that matters is that if require
/include
are used inside a function, then the code in the included file is considered to be inside that function as well.
So your mentions of $name
in this file all refer to the same local variable, in the scope of whatever function this file is included from. But they don't refer to the global variable $name
. Function declarations, incidentally, still create global functions, because PHP has no such thing as nested/local functions.
The simplest solution is to get out of the habit of using global variables, and then you won't have to worry about this problem. In this case, you're defining a function, so you can pass that function as much information as it needs; then, if you need to call it based on a different combination, you can, rather than having to redefine a global variable to suit each case.
function form_submit($name, $description, $crn, $password_init, $password_rt)
{
if (empty($name) || empty($description) || empty($crn) || empty($password_init) || empty($password_rt))
{
echo "<b style='color:red'>* $name</b><br>";
echo "<b style='color:red'>* $description</b><br>";
echo "<b style='color:red'>* $crn</b><br>";
echo "<b style='color:red'>* $password_init</b><br>";
echo "<b style='color:red'>* $password_rt</b><br>";
}
}
if(isset($_POST['Submit']))
{
form_submit($name, $description, $crn, $password_init, $password_rt);
}
Or even:
if(isset($_POST['Submit']))
{
form_submit($_POST['name'], $_POST['description'], $_POST['crn'], $_POST['password_init'], $_POST['password_rt']);
}