2013-02-20 77 views
0

*编辑/终止液/工作代码来自外部文件的PHP变量?

所以,这是我的一个朋友帮我想出了。

这是我在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 = "[email protected]"; 

    //header 
    $headers = "From:" . $from; 

    //send the email 
    mail($to,$subject,$message,$headers); 
?> 

原来,有一个简单的理由它不工作...... JavaScript似乎不处理PHP!

所以“do_stuff.php”(以前名为sendmail.php)从未用echo JURI :: base();执行。

尽管如此。 var = $ this-> item ...也试图从PHP变量中获取数据,这些数据不工作。因此,要对付那些放入隐藏输入表单的变量的值,以通过getObjectById检索它们。

就像我的朋友所说的,不知道这是最优雅还是最复杂的解决方案......但它确实有效并填补了它的目的。

但是,如果有人有实现这一目标的一个更好的更“正确”的方式,我所有的耳朵:)

谢谢@jack的帮助!还有其他人在未来对这个主题做出贡献。

- 原来的职位 -

仍在学习PHP和Joomla和K2。现在一直坐在upp上,试图了解在使用fb:comments发表评论时,如何让特定作者收到电子邮件。

到目前为止好... FB.event.subscribe comment.create acting without action from user

现在,唯一缺少的是referens变量“$用品 - >作者 - >名称”。由于这是在我打电话的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> 

原始文件(item.php)可用且这是在“sendmail.php”文件

<?php 
    if ($item->author->name == "Firstname1 Lastname1"){ 
     $to = "[email protected]"; 
    }else if ($item->author->name == "Firstname2 Lastname2"){ 
     $to = "[email protected]"; 
    }; 

    $subject = "New comment"; 
    $message = "A new comments has been made."; 
    $from = "[email protected]"; 
    $headers = "From:" . $from; 
    mail($to,$subject,$message,$headers); 
?> 

我不我知道如何让$ item-> author->名字起作用。由于我需要确保以某种方式检查名称(因为它显示在生成的页面上,我必须能够以某种方式使用它)来指定要发送到哪个电子邮件。

我不知道这是否已经被问到,但我甚至不知道要找什么来让我在这里开始。我无法想象这会难以解决(如果你只知道你需要改变什么)。 :)

回答

3

您可以尝试将作者姓名作为参数传递给您的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 = "[email protected]"; 
    }else if ($author == "Firstname2 Lastname2"){ 
     $to = "[email protected]"; 
    }; 

    $subject = "New comment"; 
    $message = "A new comments has been made."; 
    $from = "[email protected]"; 
    $headers = "From:" . $from; 
    mail($to,$subject,$message,$headers); 
?> 

同样,这没有经过测试,但应该给你一个想法。既然你使用Joomla,你也应该看看Joomla的com_mailto组件,它可能会也可能不会更容易。您可以通过“通过ajax将参数传递给外部PHP脚本”或者沿着这些行搜索更多信息。

而且,这里是为jQuery ajax

+0

这并没有做一个参考,但它看起来像你在正确的轨道上:)我仍然不知道什么时候我在寻找这个输入什么,当你将一个变异传递给另一个呼叫时,称为什么? $ .post不起作用,我不知道为什么。这就是为什么我改变我为$ .get(这只是我的一部分,但它工作:))。我可以做些什么来将“var name = $ this-> item-> author-> name”转移到“sendmail.php”? – axelra82 2013-02-21 09:45:56

+0

我会看看Joomla,看看我能弄清楚什么。 – Jack 2013-02-21 17:48:42

+0

感谢您的帮助!我仍然无法获得变长的$ item-> author-> name传递给sendmail.php ...我会继续寻找:D如果你发现如何去做,请让我知道;) 当所有工作都完成并开始工作时,我会在这里发布完整的工作代码。 – axelra82 2013-02-22 11:33:01