Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing php variable from one file to another?

Tags:

variables

php

I have a variable set in my main file (main.php), and need the second file (uploads.php) to reference the variable as it is set in the first file. It is returning undefined right now tho.

The second file is loaded with $.load into the first file: code example below -

Main.php Contents:

<?php $accountName = get_option('account_name'); ?>

<div id="uploads"></div>

<a href="#" onclick="loadUploadsFile()">Load Your Playlist</a>

function loadUploadsFile() {
     jQuery('#uploads').load('uploads.php');
}


Uploads.php file contents

<?php echo $account_name; ?>    <== returns undefined

$url = 'http://www.somewebsite.com/' . $accountName . '/page/'

/* more code below running a query/loop etc. */


As you may be able to tell, I want Uploads.php to reference the variable decleration in Main.php but is is not pulling the value, it is just returning undefined. Uploads.php loads into the uploads div, but without the account name set the content is just blank.

Would I need to pass the variable to Uploads.php through ajax? I've tried session variables but couldn't get that to work. I was trying an ajax request but I am new to it so couldn't get that nailed. Any help would be great.

like image 265
EHerman Avatar asked Dec 11 '25 08:12

EHerman


1 Answers

Session variables should work after all. Make sure you have the following:

In Main.php

session_start();

$_SESSION["account_name"] = "test";

In Uploads.php

echo $_SESSION["account_name"];
like image 196
Praxis Ashelin Avatar answered Dec 12 '25 23:12

Praxis Ashelin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!