外部文件中的PHP变量?
*编辑/完成解决方案/工作代码 这就是我的一个朋友帮我想出来的 以下是我在K2“items.php”文件中使用的部分:外部文件中的PHP变量?,php,facebook,variables,if-statement,joomla-k2,Php,Facebook,Variables,If Statement,Joomla K2,*编辑/完成解决方案/工作代码 这就是我的一个朋友帮我想出来的 以下是我在K2“items.php”文件中使用的部分: 您可以尝试在ajax调用中将作者姓名作为参数传递。大致如下: FB.Event.subscribe('comment.create', function (response) { var name = $item->author->name; $.get('<?php echo JURI::base(); ?>sendmail.
您可以尝试在ajax调用中将作者姓名作为参数传递。大致如下:
FB.Event.subscribe('comment.create', function (response) {
var name = $item->author->name;
$.get('<?php echo JURI::base(); ?>sendmail.php'), new {'authorName': name};
});
您还可以使用$.post将参数发送到sendmail脚本
注意:这是未经测试和记忆,但希望它能为您指明正确的方向。我上次与Joomla合作也有一段时间了,可能有更好的Joomla特定方法来实现这一点
编辑:下面是一个使用POST将变量传递给sendmail脚本的示例:
FB.Event.subscribe('comment.create', function (response) {
var name = $item->author->name;
$.ajax({
type: "POST",
url:'<?php echo JURI::base(); ?>sendmail.php'),
data: authorName,
cache: false,
});
});
FB.Event.subscribe('comment.create',函数(response){
变量名称=$item->author->name;
$.ajax({
类型:“POST”,
url:'sendmail.php'),
数据:authorName,
cache:false,
});
});
…并在sendmail.php文件中:
<div class="fb-comments" data-href="<?php echo JURI::current(); ?>" data-num-posts="8" notify="true" data-width="580"></div>
<input id="authname" style="display: none;" type="text" value="<?php echo $this->item->author->name; ?>" />
<input id="authmail" style="display: none;" type="text" value="<?php echo $this->item->author->email; ?>" />
<input id="link" style="display: none;" type="text" value="<?php echo JURI::current(); ?>" />
<script>
window.fbAsyncInit = function() {
FB.Event.subscribe('comment.create', function (response) {
var commentQuery = FB.Data.query("SELECT text, fromid FROM comment WHERE post_fbid='" + response.commentID +
"' AND object_id IN (SELECT comments_fbid FROM link_stat WHERE url='" + response.href + "')");
var userQuery = FB.Data.query("SELECT name FROM user WHERE uid in (select fromid from {0})", commentQuery);
FB.Data.waitOn([commentQuery, userQuery], function () {
var commentRow = commentQuery.value[0];
var userRow = userQuery.value[0];
console.log(userRow.name + " (id: " + commentRow.fromid + ") posted the comment: " + commentRow.text);
trackcomments(response['commentID'], response['href'], 'create', commentRow.text, userRow.name, commentRow.fromid);
});
});
};
function trackcomments(_commentid, _address, _action, _commentMessage, _userName, _userId) {
var authname = document.getElementById('authname').value;
var authmail = document.getElementById('authmail').value;
var link = document.getElementById('link').value;
$.ajax({
type: 'POST',
url: 'http://mydomain.com/dostuff.php',
data: {'commentMessage': _commentMessage, 'userName': _userName, 'authname': authname, 'authmail': authmail, 'link': link},
cache: false
});
};
</script>
<?php
//creating an $author variable and populating it from $_POST
$author = $_POST['authorName'];
if ($author == "Firstname1 Lastname1"){
$to = "author1@mydomain.com";
}else if ($author == "Firstname2 Lastname2"){
$to = "author2@mydomain.com";
};
$subject = "New comment";
$message = "A new comments has been made.";
$from = "admin@mydomain.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
?>
同样,这是未经测试,但应该给你一个想法。由于您使用的是Joomla,您还应该查看Joomla的com\u mailto
组件,它可能更容易,也可能不容易。您可以通过“通过ajax将参数传递到外部PHP脚本”或类似的方式来搜索更多信息
此外,这里还有一个参考,您可以尝试在ajax调用中将作者姓名作为参数传递。大致如下:
FB.Event.subscribe('comment.create', function (response) {
var name = $item->author->name;
$.get('<?php echo JURI::base(); ?>sendmail.php'), new {'authorName': name};
});
您还可以使用$.post将参数发送到sendmail脚本
注意:这是未经测试和记忆,但希望它能为您指明正确的方向。我上次与Joomla合作也有一段时间了,可能有更好的Joomla特定方法来实现这一点
编辑:下面是一个使用POST将变量传递给sendmail脚本的示例:
FB.Event.subscribe('comment.create', function (response) {
var name = $item->author->name;
$.ajax({
type: "POST",
url:'<?php echo JURI::base(); ?>sendmail.php'),
data: authorName,
cache: false,
});
});
FB.Event.subscribe('comment.create',函数(response){
变量名称=$item->author->name;
$.ajax({
类型:“POST”,
url:'sendmail.php'),
数据:authorName,
cache:false,
});
});
…并在sendmail.php文件中:
<div class="fb-comments" data-href="<?php echo JURI::current(); ?>" data-num-posts="8" notify="true" data-width="580"></div>
<input id="authname" style="display: none;" type="text" value="<?php echo $this->item->author->name; ?>" />
<input id="authmail" style="display: none;" type="text" value="<?php echo $this->item->author->email; ?>" />
<input id="link" style="display: none;" type="text" value="<?php echo JURI::current(); ?>" />
<script>
window.fbAsyncInit = function() {
FB.Event.subscribe('comment.create', function (response) {
var commentQuery = FB.Data.query("SELECT text, fromid FROM comment WHERE post_fbid='" + response.commentID +
"' AND object_id IN (SELECT comments_fbid FROM link_stat WHERE url='" + response.href + "')");
var userQuery = FB.Data.query("SELECT name FROM user WHERE uid in (select fromid from {0})", commentQuery);
FB.Data.waitOn([commentQuery, userQuery], function () {
var commentRow = commentQuery.value[0];
var userRow = userQuery.value[0];
console.log(userRow.name + " (id: " + commentRow.fromid + ") posted the comment: " + commentRow.text);
trackcomments(response['commentID'], response['href'], 'create', commentRow.text, userRow.name, commentRow.fromid);
});
});
};
function trackcomments(_commentid, _address, _action, _commentMessage, _userName, _userId) {
var authname = document.getElementById('authname').value;
var authmail = document.getElementById('authmail').value;
var link = document.getElementById('link').value;
$.ajax({
type: 'POST',
url: 'http://mydomain.com/dostuff.php',
data: {'commentMessage': _commentMessage, 'userName': _userName, 'authname': authname, 'authmail': authmail, 'link': link},
cache: false
});
};
</script>
<?php
//creating an $author variable and populating it from $_POST
$author = $_POST['authorName'];
if ($author == "Firstname1 Lastname1"){
$to = "author1@mydomain.com";
}else if ($author == "Firstname2 Lastname2"){
$to = "author2@mydomain.com";
};
$subject = "New comment";
$message = "A new comments has been made.";
$from = "admin@mydomain.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
?>
同样,这是未经测试,但应该给你一个想法。由于您使用的是Joomla,您还应该查看Joomla的com\u mailto
组件,它可能更容易,也可能不容易。您可以通过“通过ajax将参数传递到外部PHP脚本”或类似的方式来搜索更多信息
另外,这里有一个引用,它没有这样做,但看起来你的思路是正确的:)我仍然不知道在搜索时键入什么,当你将一个变量传递给另一个调用时,它叫什么?美元的帖子不起作用,我不知道为什么。这就是为什么我将I改为$.get(这与我的角色相去甚远,但它成功了:)。还有其他人知道我能做些什么来将var name=$this->item->author->name“转移”到“sendmail.php”吗?我会看看Joomla,看看我能找到什么。谢谢你的帮助!我仍然无法将variabel$item->author->name传递到sendmail.php。。。我会继续找的:D如果你知道怎么做,请告诉我;)当所有的工作都完成后,我将在这里发布完整的工作代码。第二个代码块中有一个输入错误:data:authorName,应该是:data:{authorName:name};否则它看起来就足够有说服力了,如果它仍然不起作用,请尝试在ajax调用之前和sendmail.php脚本中输出变量,看看它们在哪里丢失了。请查看原始帖子,以获得完成/工作的解决方案。但没有这样做,但是看起来你的思路是对的:)我仍然不知道在搜索这个时应该键入什么,当你把一个变量传递给另一个调用时,它叫什么?美元的帖子不起作用,我不知道为什么。这就是为什么我将I改为$.get(这与我的角色相去甚远,但它成功了:)。还有其他人知道我能做些什么来将var name=$this->item->author->name“转移”到“sendmail.php”吗?我会看看Joomla,看看我能找到什么。谢谢你的帮助!我仍然无法将variabel$item->author->name传递到sendmail.php。。。我会继续找的:D如果你知道怎么做,请告诉我;)当所有的工作都完成后,我将在这里发布完整的工作代码。第二个代码块中有一个输入错误:data:authorName,应该是:data:{authorName:name};否则它看起来就足够有说服力了,如果它仍然不起作用,请尝试在ajax调用之前和sendmail.php脚本中输出变量,看看它们在哪里丢失了。请查看原始文章以获得完成/工作的解决方案。