你可以使用$ _ GET变量,包括在PHP文件时?有没有可能,甚至从一个AJAX调用? [英] Can you use $_GET variables when including a file in PHP? Is it possible, even from an AJAX call?

查看:104
本文介绍了你可以使用$ _ GET变量,包括在PHP文件时?有没有可能,甚至从一个AJAX调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个处理发送确认电子邮件的PHP文件。我也有,我用AJAX做各种更新日历。当AJAX调用更新文件,它会更新与新的信息的数据库,我想确认电子邮件发送出去。

I have a PHP file that handles sending out confirmation emails. I also have a calendar that I use AJAX to do various updates. When AJAX calls the update file, it updates the database with the new information, and I want confirmation emails to be sent out.

所以从Ajax调用PHP文件里,我想我应该包括(电子邮件sender.php阶段= confirm2a和放大器;工作= {$ slot-> JOB_ID} 这就要求电子邮件PHP页面,用$ _GET变量告诉它要发送的电子邮件和谁。

So from inside the php file that the AJAX calls, I figured I should include("email-sender.php?stage=confirm2a&job={$slot->job_id} which calls the email php page, with the $_GET variables that tell it which emails to send and to who.

但由于某些原因,我不能得到包括当您使用工作?key = value的 $ _GET连接字符串对。 PHP.net告诉我,你可以在一个包含使用$ _ GET变量,但是当我建立了一个简单的测试,它似乎并没有工作。

But for some reason, I can't get the include to work when you use ?key=value $_GET pairs attached to the string. PHP.net tells me you can use $_GET variables in an include, but when I set up a simple test, it doesn't appear to work.

我的测试页面有一个链接,单击时提交Ajax调用的页面以及包含一个变量农场,这等于值动物的数据。像这样的:

My test page has one link, that when clicked submits an ajax call to a page along with data containing one variable "farm" which equals the value "animal". Like this:

$("a.testlink").click(function() {
    var mydata = "farm=animal";
    $.ajax({
        url: "ajaxPHP.php",
        data: mydata,
        success: function(rt) {
            alert(rt);
        }
});

所以ajaxPHP.php说:

So ajaxPHP.php says:

if($_GET['farm']) {
    $var = $_GET['farm'];
    echo $var;
}

在这一点上,成功警报显示的动物的链接被点击时。这是正确的。

At this point, the success alert shows "animal" when the link is clicked. That's right.

但是,如果我改变ajaxPHP.php这样:

But if I change ajaxPHP.php to this:

if($_GET['farm']) {
    $var = $_GET['farm'];
    include("ajaxInclude.php?farm={$var}");
}

和有一个名为ajaxInclude.php文件,上面写着:

And have a file called ajaxInclude.php that says:

if($_GET['farm']) {
    $var = $_GET['farm'];
    echo $var;
}

后来,当我点击我得到一个空的警告链接。因此,包括不与追加到末尾的查询字符串工作。

Then when I click the link I get an empty alert. So the include doesn't work with the query string appended to the end.

任何帮助吗?

加成

所以现在我有以下几点:

So now I have the following:

$stage = "confirm2a";
include("email-sender.php");
$stage = "confirm2b";
include("email-sender.php");

然后在电子邮件sender.php,显然是有很多code这样的:

And then in email-sender.php, obviously there is a lot of code like:

if($stage == "confirm2a") { 
   email Person 1 etc...
}
if($stage == "confirm2b") {
    email Person 2 etc...
}

但是当我运行该脚本,只有1人接收电子邮件,并且只有一次。不知道为什么...

But when I run the script, only Person 1 receives the email, and only once. Not sure why...

推荐答案

您可以在包括脚本中处理的变量,而不是追加到他们的包括路径本身:

You can handle the variables within your included script, rather than appending them onto the include path itself:

$var = "foo";
include("script.php");

- script.php的内容 -

-- contents of script.php --

echo $var; // 'foo'

在我看来这是干净多了反正。您可能希望进行检查,以确保呼应任何变量之前,在的script.php 存在的价值。

In my opinion this is much cleaner anyway. You may desire to check to make sure the values exist within script.php before echoing out any variables.

这篇关于你可以使用$ _ GET变量,包括在PHP文件时?有没有可能,甚至从一个AJAX调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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