facebook - PHP variable from external file? -
facebook - PHP variable from external file? -
*edit / finished solution / working code
so, friend of mine helped me come with.
here part utilize in 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 comment post_fbid='" + response.commentid + "' , object_id in (select comments_fbid link_stat url='" + response.href + "')"); var userquery = fb.data.query("select name user uid in (select fromid {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 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 do_stuff.php:
<?php //handle weird letters , stuff setlocale(lc_time, 'swedish'); //creating $author variable , populating $_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 entry $link#comments\n\nuse above link reply on comment."; //who mail service $from = "admin@mydomain.com"; //header $headers = "from:" . $from; //send email mail($to,$subject,$message,$headers); ?>
turns out, there simple reason wasn't working... javascript doesn't seem handle php!
so "do_stuff.php" (earlier named sendmail.php) never executed echo juri::base();.
even though. var = $this->item... trying info php variables wasn't working. so, combat values of variables set in hidden input forms retrieve them thru getobjectbyid.
like friend stated, don't know if elegant or sophisticated solution... trick , fills it's purpose.
however, if has improve more "correct" way of achieving this, i'm ears :)
thank @jack help! , else contributing subject in future.
- original post -
still learning php , joomla , k2. been sitting upp days trying figure out how can have specific authors receive emails when comments made using fb:comments.
so far good... fb.event.subscribe comment.create acting without action user
now, thing missing referens variable "$item->author->name". since usable in original file (item.php) i'm calling sendmail.php
<script> window.fbasyncinit = function() { /* events registered */ fb.event.subscribe('comment.create', function (response) { $.get('<?php echo juri::base(); ?>sendmail.php'); }); }; </script>
and "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 can $item->author->name work. since need create sure somehow checks see name (since it's showing on generated page have able utilize somehow) specify email send to.
i have no thought if has been asked, don't know search me started here. can't imagine hard solve (if know need change). :)
you can seek passing author name parameter in ajax call. 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 sendmail script should able access passed authorname
parameter...
if (authorname == "firstname1 lastname1"){...
you utilize $.post send parameter sendmail script.
note: untested , memory, point in right direction. it's been while since lastly worked joomla, , there improve joomla-specific way accomplish this.
edit: here's illustration of using post pass variable 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 sendmail.php file:
<?php //creating $author variable , populating $_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 untested, should give idea. since you're using joomla should joomla's com_mailto
component, may or may not easier. can search farther info "pass parameter external php script via ajax" or along lines.
also, here's reference jquery ajax
php facebook variables if-statement joomla-k2
Comments
Post a Comment