PHP variable from external file?

好久不见. 提交于 2019-12-02 09:41:56

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

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