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...
There are, at least, two alternatives to solve this:
1)
Then, in ajaxInclude.php:
2)
$_GET
is a global variable, so can be used anywhere within your php script, including any included scriptsYou can handle the variables within your included script, rather than appending them onto the include path itself:
-- contents of 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.