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...

rhodesjason
  • 4,904
  • 9
  • 43
  • 59

3 Answers3

9

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.

Sampson
  • 265,109
  • 74
  • 539
  • 565
  • 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"); – rhodesjason Jan 29 '10 at 16:49
  • It will use the most recently declared value. So, yes. – Sampson Jan 29 '10 at 16:50
  • Hmm. I assumed that but it's not working. See my edit above... thanks so much for your help! – rhodesjason Jan 29 '10 at 16:53
  • I just added my edit at the bottom of the original post. Any thoughts? – rhodesjason Jan 29 '10 at 16:56
  • 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. – rhodesjason Jan 29 '10 at 17:28
0

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

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

Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99