在 PHP 中包含文件时可以使用 $_GET 变量吗?是否有可能,即使来自 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 文件中,我想我应该 include("email-sender.php?stage=confirm2a&job={$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 调用以及包含一个变量farm"的数据,该变量等于值animal".像这样:
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;
}
此时,点击链接时成功提示显示animal".没错.
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");
然后在email-sender.php中,显然有很多类似的代码:
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.
相关文章