views:

386

answers:

3

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  A: 

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.

Jonathan Sampson
This is great but one follow up question: Can I include the same script twice or more, and pass different vars to it each time? Like $var = "foo"; include("script.php"); $var = "bar"; include("script.php");
Jason Rhodes
It will use the most recently declared value. So, yes.
Jonathan Sampson
Hmm. I assumed that but it's not working. See my edit above... thanks so much for your help!
Jason Rhodes
@JasonRhodes: I just tested it locally and it worked.
Jonathan Sampson
I just added my edit at the bottom of the original post. Any thoughts?
Jason Rhodes
Figured it out! It was because there were redirects in place in the included file, so once it finished the first it redirected and didn't continue on. Solved it now. Thanks again, Jonathan.
Jason Rhodes
A: 
Joel Alejandro
A: 

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

adam