PHP variable from external file?

左心房为你撑大大i 提交于 2019-12-31 05:41:53

问题


*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). :)


回答1:


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



来源:https://stackoverflow.com/questions/14992143/php-variable-from-external-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!