将php变量从一个文件传递到另一个文件? [英] Passing php variable from one file to another?

查看:50
本文介绍了将php变量从一个文件传递到另一个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主文件 (main.php) 中设置了一个变量,需要第二个文件 (uploads.php) 来引用在第一个文件中设置的变量.它现在返回 undefined tho.

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.

第二个文件用 $.load 加载到第一个文件中:下面的代码示例 -

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

Main.php 内容:

<?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 文件内容

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

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

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


正如您可能知道的那样,我希望 Uploads.php 引用 Main.php 中的变量声明,但并没有提取该值,它只是返回 undefined.Uploads.php 加载到uploads div,但没有设置帐户名,内容只是空白.

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.

我是否需要通过 ajax 将变量传递给 Uploads.php?我试过会话变量,但无法让它工作.我正在尝试一个 ajax 请求,但我是新手,所以无法解决这个问题.任何帮助都会很棒.

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.

推荐答案

您可以在 jQuery 调用中将其作为 GET 参数传递,例如:

You could pass it as a GET parameter in your jQuery call like:

jQuery('#uploads').load('uploads.php?account=<?php echo($accountName);?>');

然后在您的 uploads.php 文件中从如下请求中获取它:

And then in your uploads.php file get it from the request like:

$accountName = $_GET['account'];

其他一些选项是将其存储在会话中、设置 cookie 或使用 jQuery .post 将其作为 POST 数据发送.

Some other options are storing it in the session, setting a cookie, or using jQuery .post to send it as POST data.

这篇关于将php变量从一个文件传递到另一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆