Can you use $_GET variables when including a file

2019-02-25 05:41发布

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.

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.

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.

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);
        }
});

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.

But if I change ajaxPHP.php to this:

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

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.

Any help?

ADDITION

So now I have the following:

$stage = "confirm2a";
include("email-sender.php");
$stage = "confirm2b";
include("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...
}

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

3条回答
我命由我不由天
2楼-- · 2019-02-25 05:52

There are, at least, two alternatives to solve this:

1)


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

Then, in ajaxInclude.php:


if($farm) {
    $var = $farm;
    echo $var;
}

2)


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

查看更多
何必那么认真
3楼-- · 2019-02-25 05:56

$_GET is a global variable, so can be used anywhere within your php script, including any included scripts

查看更多
倾城 Initia
4楼-- · 2019-02-25 06:05

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

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

-- contents of script.php --

echo $var; // 'foo'

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.

查看更多
登录 后发表回答