来自外部文件的PHP变量? [英] PHP variable from external file?

查看:152
本文介绍了来自外部文件的PHP变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

*编辑/完成解决方案/工作代码



所以,这是我的一位朋友帮我提出的。



以下是我在K2items.php文件中使用的部分:

 < div class =fb-commentsdata-href =<?php echo JURI :: current();?> data-num-posts =8notify =truedata-width =580>< / div> 

< input id =authnamestyle =display:none; type =textvalue =<?php echo $ this-> item-> author-> name;?> />
< input id =authmailstyle =display:none; type =textvalue =<?php echo $ this-> item-> author-> email;?> />
< input id =linkstyle =display:none; type =textvalue =<?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 +)发表评论:+ commentRow.text);
trackcomments(response ['commentID'],response ['href'],'create',commentRow.text,userRow.name, commentRow.fromid);
});
});
};

函数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({
类型:'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 

//处理一些奇怪的字母和东西
setlocale(LC_TIME,'swedish');

//创建一个$ author变量并从$ _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());

//获取作者电子邮件
$ to = $ authoremail;

//电子邮件主题
$ subject =在mydmomain.com上发布新评论;

//电子邮件内容
$ message =On $ date $ userName write\\\
\\\
$ commentMessage\\\
n \\ your your entry $ link#comments \ nn \\ n使用上面的链接回答评论。;

//邮件来自
$来自=admin@mydomain.com;

// header
$ headers =From:。从$;

//发送电子邮件
邮件($ to,$ subject,$ message,$ headers);
?>

原来,有一个简单的原因它无法正常工作...... JavaScript似乎没有处理PHP!



所以do_stuff.php(之前名为sendmail.php)从未使用echo JURI :: base();.

尽管如此。 var = $ this-> item ...也试图从PHP变量中获取数据,这些数据无法正常工作。因此,要通过getObjectById来解决那些放入隐藏输入形式的变量的值,以便通过getObjectById来检索它们。



像我的朋友说的那样,不知道这是不是最优雅或最复杂的解决方案...但它确实可以解决问题。



但是,如果某人有更好的正确方法来实现这一目标,我全是耳朵:)



谢谢你@jack的帮助!以及其他任何人在将来为此主题做出贡献。



- 原始邮寄 -



还在学习PHP和Joomla和K2。一直坐在upp上几天试图找出如何让特定作者在使用fb:comments发表评论时收到电子邮件。



到目前为止一直这么好...... b $ b FB.event.subscribe comment.create acting没有来自用户的操作



现在,唯一缺少的是对变量$ item-> author-> name的引用。因为这可以在原始文件(item.php)中使用,我在这里调用sendmail.php

 < script> ; 
window.fbAsyncInit = function(){

/ *注册的所有事件* /
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;
}否则if($ item-> author-> name ==Firstname2 Lastname2){
$ to =author2@mydomain.com;
};

$ subject =新评论;
$ message =已发表新评论。;
$ from =admin@mydomain.com;
$ headers =从:。从$;
邮件($ to,$ subject,$ message,$ headers);
?>

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



我不知道是否已经问过这个问题,但我甚至不知道该搜索什么让我从这里开始。我无法想象这将难以解决(如果你只知道你需要改变什么)。 :)

解决方案

您可以尝试将作者姓名作为参数传递给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({
类型:POST,
url:'<?php echo JURI :: base();?> sendmail.php'),
数据:authorName,
cache:false,
});
});

...并在你的sendmail.php文件中:

 <?php 
//创建一个$ author变量并从$ _POST
$ author = $ _POST ['authorName']填充它;

if($ author ==Firstname1 Lastname1){
$ to =author1@mydomain.com;
} else if($ author ==Firstname2 Lastname2){
$ to =author2@mydomain.com;
};

$ subject =新评论;
$ message =已发表新评论。;
$ from =admin@mydomain.com;
$ headers =从:。从$;
邮件($ to,$ subject,$ message,$ headers);
?>

这是未经测试的,但应该给你一个想法。由于您使用Joomla,您还应该查看Joomla的 com_mailto 组件,它可能会或可能不会更容易。您可以使用通过ajax将参数传递给外部PHP脚本或其他内容来搜索更多信息。



此外,这里是 jQuery ajax


*EDIT / FINISHED SOLUTION / WORKING CODE

So, this is what a friend of mine helped me come up with.

Here is the part I use in my K2 "items.php" file:

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

And this is the 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);
?>

Turns out, there was a simple reason it wasn't working... JavaScript doesn't seem to handle PHP!

So the "do_stuff.php" (earlier named sendmail.php) was never executed with the echo JURI::base();.

Even then though. The var = $this->item... was also trying to get data from PHP variables which wasn't working. So, to combat that the values of those variables where put in hidden input forms to retrieve them thru getObjectById.

Like my friend stated, don't know if this is the most elegant or sophisticated solution... but it does the trick and fills it's purpose.

However, if someone has a better more "correct" way of achieving this, I'm all ears :)

Thank you @jack for your help! And anyone else contributing to this subject in the future.

- ORIGINAL POST -

Still learning about PHP and Joomla and K2. Been sitting upp for days now trying to figure out how I can have specific authors receive emails when comments are made using fb:comments.

So far so good... FB.event.subscribe comment.create acting without action from user

Now, the only thing missing is the referens to the variable "$item->author->name". Since this is usable in the original file (item.php) where I'm calling for the 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>

and this is the "sendmail.php" file

<?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);
?>

I don't know how I can get the $item->author->name to work. Since I need to make sure that it somehow checks to see what the name is (since it's showing on the generated page I have to be able to use it somehow) to specify which email to send TO.

I have no idea if this has already been asked, but I don't even know what to search for to get me started here. I can't imagine that this would be to difficult to solve (if you only know what you need to change). :)

解决方案

You can try passing the author name as a parameter in your ajax call. Something along these lines:

FB.Event.subscribe('comment.create', function (response) {
     var name = $item->author->name;
        $.get('<?php echo JURI::base(); ?>sendmail.php'), new {'authorName': name};
    });

Then in your sendmail script you should be able to access the passed authorName parameter...

if (authorName == "Firstname1 Lastname1"){...

You could also use $.post to send the parameter to the sendmail script.

Note: This is untested and from memory, but hopefully it will point you in the right direction. It's also been a while since I last worked with Joomla, and there is likely a better Joomla-specific way to accomplish this.

EDIT: here's an example of using POST to pass the variable to the sendmail script:

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, 
             });
});

...and in your sendmail.php file:

<?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);
?>

Again this is untested, but should give you an idea. Since you're using Joomla you should also look into Joomla's com_mailto component, it may or may not be easier. You can search for further info with "pass parameter to external PHP script via ajax" or something along those lines.

Also, here's a reference for jQuery ajax

这篇关于来自外部文件的PHP变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆