2013-02-20 76 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