[英]PHP variable from external file?
*编辑/完成的解决方案/工作代码
因此,这是我的一个朋友帮助我提出的。
这是我在K2“ items.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>
这是do_stuff.php:
<?php
//Handle some weird letters and stuff
setlocale(LC_TIME, 'swedish');
//creating an $author variable and populating it from $_POST
$author = $_POST['authname'];
$authoremail = $_POST['authmail'];
$link = $_POST['link'];
$commentMessage = $_POST['commentMessage'];
$userName = $_POST['userName'];
$date = strftime('%A %e %b %Y %H.%M', time());
//getting author email
$to = $authoremail;
//subject of email
$subject = "New comment posted on mydmomain.com";
//email content
$message = "On $date $userName wrote\n\n$commentMessage\n\non your entry $link#comments\n\nUse the above link to answer on the comment.";
//who the mail is from
$from = "admin@mydomain.com";
//header
$headers = "From:" . $from;
//send the email
mail($to,$subject,$message,$headers);
?>
事实证明,有一个简单的原因它无法正常工作……JavaScript似乎无法处理PHP!
因此,从未使用echo JURI :: base();执行“ do_stuff.php”(以前名为sendmail.php)。
即使那样。 var = $ this-> item ...也在尝试从无效的PHP变量中获取数据。 因此,要避免将这些变量的值放在隐藏的输入形式中,以通过getObjectById检索它们。
就像我的朋友所说的那样,不知道这是最优雅还是最精致的解决方案……但是它能解决问题并达到目的。
但是,如果有人有更好,更“正确”的方法来实现这一目标,那么我无不为之:)
谢谢@jack的帮助! 以及将来任何其他对此主题做出贡献的人。
-原帖-
仍在学习PHP,Joomla和K2。 现在坐了几天,试图弄清楚当使用fb:comments发表评论时,特定作者如何收到电子邮件。
到目前为止一切顺利... FB.event.subscribe comment.create无需用户采取行动即可执行
现在,唯一缺少的是引用变量“ $ item-> author-> name”。 由于这在原始文件(item.php)中可用,因此我在其中调用sendmail.php
<script>
window.fbAsyncInit = function() {
/* All the events registered */
FB.Event.subscribe('comment.create', function (response) {
$.get('<?php echo JURI::base(); ?>sendmail.php');
});
};
</script>
这是“ sendmail.php”文件
<?php
if ($item->author->name == "Firstname1 Lastname1"){
$to = "author1@mydomain.com";
}else if ($item->author->name == "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);
?>
我不知道如何获得$ item-> author-> name的名称。 因为我需要确保它以某种方式检查以查看名称(由于它已经显示在生成的页面上,所以我必须能够以某种方式使用它)以指定发送至的电子邮件。
我不知道是否已经有人问过这个问题,但是我什至不知道要寻找什么才能让我从这里开始。 我无法想象这将很难解决(如果您只知道需要更改的内容)。 :)
您可以尝试在ajax调用中将作者姓名作为参数传递。 遵循以下原则:
FB.Event.subscribe('comment.create', function (response) {
var name = $item->author->name;
$.get('<?php echo JURI::base(); ?>sendmail.php'), new {'authorName': name};
});
然后,在您的sendmail脚本中,您应该能够访问传递的authorName
参数...
if (authorName == "Firstname1 Lastname1"){...
您也可以使用$ .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,
});
});
...以及您的sendmail.php文件中:
<?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_mailto
组件,它可能会或可能不会更简单。 您可以使用“通过ajax将参数传递给外部PHP脚本”或类似方式搜索更多信息。
另外,这是jQuery ajax的参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.