1

I set a Super global variable $GLOBALS['Type']="JobSeeker" but in another file that variable is not accessed While Running Program am getting Undefined Index:Type.

Page1.php:

$con = mysqli_connect("localhost","root","","job1");
// Specify the query to execute
$sql = "select * from JobSeeker_registration where JobSeekerId='".$ID."'  ";
// Execute query
$result = mysqli_query($con,$sql);
// Loop through each records 
$row = mysqli_fetch_array($result);


$GLOBALS['Name']=$row['JobSeekerName'];

$GLOBALS['Email']=$row['Email'];

$GLOBALS['Approval']="Confirm";

$GLOBALS['Type']="JobSeeker";
.
.
.
.
include '../Mailing/MailSending.php';

MailSending.php:

if($GLOBALS['Type']=="JobSeeker")  //Here is Error:-Undefined Index of Type
{
    if($GLOBALS['Approval']=="Pending")
    {
        $UserName=$GLOBALS['Email'];
        $mail->addAddress($UserName, $GLOBALS['Name']);
        $mail->Subject = 'Registration Mail';
        $mail->msgHTML(file_get_contents("Mailing/Pending.html"), __DIR__);
        $GLOBALS['Email']="";
        $GLOBALS['Type']="";
        $GLOBALS['Name']="";
        $GLOBALS['Approval']="";
    }
    elseif ($GLOBALS['Approval']=="Confirm") 
    {
        $UserName=$GLOBALS['Email'];
        $mail->addAddress($UserName, $GLOBALS['Name']);
        $mail->Subject = 'Registration Confirmation Mail';
        $mail->msgHTML(file_get_contents("Mailing/Confirm.html"), __DIR__);
        $GLOBALS['Email']="";
        $GLOBALS['Type']="";
        $GLOBALS['Name']="";
        $GLOBALS['Approval']="";
    }
}

I tried with Different Names But did not get any solution.

nice_dev
  • 17,053
  • 2
  • 21
  • 35
shiva7890
  • 41
  • 1
  • 7
  • Did you include the file required for that? I think you are confusing this with sessions. What does `echo "
    ";print_r($GLOBALS);` give you?
    – nice_dev Mar 24 '19 at 05:39
  • Page2.php is the MailSending.php and Page1.php is present in one folder and MailSending.php is present in another, I included MailSending.php in Page1.php. – shiva7890 Mar 24 '19 at 06:11
  • That should work. Are you sure you haven't included this file somewhere above in the script or maybe unset the index before including this file? – nice_dev Mar 24 '19 at 06:26
  • You should avoid using $GLOBALS and find more appropriate ways of dealing with the data, https://stackoverflow.com/questions/3573847/php-global-or-globals and https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and may help. – Nigel Ren Mar 24 '19 at 07:19
  • @vivek_23 I did not unset the index but I included the same file in another file other than Page1.php, is anything wrong for including the same file in two or more files – shiva7890 Mar 24 '19 at 08:35
  • @shiva7890 So, this error doesn't occur form here and rather from that other file in which you have included `MailSending.php` since you may not have set those `$GLOBALS` there. I hope you are not trying to confuse global variables with session variables though. – nice_dev Mar 24 '19 at 08:54
  • @vivek_23 wrote the same code(Page1.php) in another file where I included MailSending.php for sending "Pending mail". It works fine There but here is getting the error. – shiva7890 Mar 24 '19 at 08:54
  • @shiva7890 With the code you provided, it should work absolutely fine without any issues. Not sure why it wouldn't. – nice_dev Mar 24 '19 at 09:00
  • okay bro thank you for responding to my question and I will try in another way without using globals – shiva7890 Mar 24 '19 at 09:05
  • @vivek_23 I got a solution,$GLOBALS must be assigned in the present file i.e., where we are including a MailSending.php, Actually, I Define $GLOBALS in Page1.php and MailSending.php is included in another PHP file that's why I got the error. – shiva7890 Mar 24 '19 at 09:39
  • @shiva7890 Reason why I said that maybe you are confusing this with session variables where you are assuming it will be automatically available once it is set on some file. – nice_dev Mar 24 '19 at 09:43
  • 1
    @vivek_23 yes, You said correctly I assumed that if a variable is set in one file those are available in all files. Thank you..! – shiva7890 Mar 24 '19 at 09:48

0 Answers0