首页 > 解决方案 > 使用 Angularjs 在 HTML 中调用 PHP 文件函数

问题描述

我对我们的一个项目有疑问,该项目是由另一位精通 Angular js 的开发人员开发的,但我不是。所以问题概述如下:我们有一个每天使用 cron 运行的报告,它是在 PHP 中直接调用的,现在有时服务器端数据库会失败,我们必须手动运行这个报告。

我有一个可用于制作按钮的 HTML 文件,但我不能直接在其中使用 PHP 函数。在这个项目中创建的所有其他 HTML 都使用 Angular js 控制器。我只想知道是否有办法让 PHP 函数在 HTML 中运行。我可以通读 angularjs,但有所有发布数据,我看不到获取 get 函数的方法。我在网上读了很多书,但我无处可去。

如果有人指出我正确的方向,我将不胜感激。

我正在寻找正确的 angularJS 成功回调语法,然后在 HTML 中调用它

PS:如果我问的是多余的问题,而不仅仅是给我一个负面的声誉,请告诉我。

它的例子:

PHP:public_html/XXXX/XXXX/php/cronHourly.php

function sendMyEmail($receiverName,$receiverEmail,$mySubject,$myMessage,$senderName,$senderEmail){
    //global $company,$companyPhone,$defaultEmail,$domain,$shopAddress,$stationeryPrefix,$siteStatus,$reportArray,$Host,$Port,$Username,$Password,$serverRoot;
$stationeryHeader = "<body bgcolor='#cccccc' leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'><style>
body {
      font-family: Arial;
       font-size: 12pt;
}
</style>
<br>
<table id='Table_01' border='0' cellpadding='0' align='center' bgcolor='#FFFFFF' width='800'>

<tr><td width='20'></td><td><br>";

$stationeryFooter = "</td><td width='20'></td></tr>

</table></body>";

    //$mail = new PHPMailer();
     $mail = new PHPMailer(true);

    $mail->IsHTML(true);
    $mail->isSMTP();  // tell the class to use SMTP
    $mail->SMTPAuth   = true; 
    //$mail->SMTPSecure  = "ssl"; //Secure conection
    $mail->Host = 'xxxx.com';
    $mail->Port = 00;
    $mail->Username = 'postmaster@xxx.xom';  // SMTP username
    $mail->Password = ''; // SMTP password
    $mail->From = 'postmaster@xxx.com';
    $mail->FromName = $senderName;
    $mail->AddReplyTo($senderEmail, $senderName); 
    $mail->AddAddress($receiverEmail, $receiverName);//send a copy to the client
    $mail->Subject = $mySubject;
    $mail->WordWrap = 80;
    $mail->AddEmbeddedImage('x.png', 'csvimg', 'csv.png'); // attach file logo.jpg, and later link to it using identfier logoimg
    //$mail->AddEmbeddedImage($serverRoot.'/images/'.$stationeryPrefix.'Header.jpg', $stationeryPrefix.'Header', $stationeryPrefix.'Header.jpg');
    //$mail->AddEmbeddedImage($serverRoot.'/images/'.$stationeryPrefix.'Footer.jpg', $stationeryPrefix.'Footer', $stationeryPrefix.'Footer.jpg');
    //$headerImage = $serverRoot.'/images/'.$stationeryPrefix.'Header.jpg';
    $mail->Body = $stationeryHeader;
    $mail->Body .= $myMessage;
    $mail->Body .= $stationeryFooter;
    if(!$mail->Send()){
        echo '<br />Message FAILED to '.$receiverEmail;
        '.$reportArray[1],$company,$defaultEmail);
    }else{
        echo '<br />Message successfully sent to '.$receiverEmail;
    }//end if mail>send
}//end function sendMyEmail


?>

HTML:

<div class="mainpage">
<div>
<div class="row" style="padding:10px">

    <p> Click on this button Reports:</p>
   <button  ng-click=????" 
                        class="btn btn-default">Send Report</button>
</div>      

</div>
</div>

可能的AngularJS:

$scope.getreportrta = function (key) {
    var req = {
        method: 'GET',
        url: '/XXXX/XXXX/php/cronHourly.php',
    };
    $http(req).then(function successCallback(response) {
        $scope.(*don't know how to fetch this*);
    }, function errorCallback(response) {

    });
}        

标签: javascriptphphtmlangularjs

解决方案


所以,我找到了我的问题的答案,如果将来有人遇到并需要帮助,我会在这里发布:

角脚本.js

$scope.sendemail = function(info){
            var req = {
                method: 'GET',
                url: 'ReportManual/manualemail.php',
            };
            //Call the URL and handle succes or error callback  --> G.H.
            $http(req).then(function successCallback(response) {
                     alert('The Report has been sent.');
                    console.log(response);

            }, function errorCallback(response) {
                alert('Report has not been sent');
                 console.log(response);
            });

        }

HTML:

<div class="row" style="padding:10px">
  <p> Click on this button to send the Reports:</p>
   <button ng-click="sendemail()">SUBMIT</button>
</div>

PHP:manualemail.php

function sendMyEmail($receiverName,$receiverEmail,$mySubject,$myMessage,$senderName,$senderEmail){
    //global $company,$companyPhone,$defaultEmail,$domain,$shopAddress,$stationeryPrefix,$siteStatus,$reportArray,$Host,$Port,$Username,$Password,$serverRoot;
$stationeryHeader = "<body bgcolor='#cccccc' leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'><style>
body {
      font-family: Arial;
       font-size: 12pt;
}
</style>
<br>
<table id='Table_01' border='0' cellpadding='0' align='center' bgcolor='#FFFFFF' width='800'>

<tr><td width='20'></td><td><br>";

$stationeryFooter = "</td><td width='20'></td></tr>

</table></body>";

    //$mail = new PHPMailer();
     $mail = new PHPMailer(true);

    $mail->IsHTML(true);
    $mail->isSMTP();  // tell the class to use SMTP
    $mail->SMTPAuth   = true; 
    //$mail->SMTPSecure  = "ssl"; //Secure conection
    $mail->Host = 'xxxx.com';
    $mail->Port = 00;
    $mail->Username = 'postmaster@xxx.xom';  // SMTP username
    $mail->Password = ''; // SMTP password
    $mail->From = 'postmaster@xxx.com';
    $mail->FromName = $senderName;
    $mail->AddReplyTo($senderEmail, $senderName); 
    $mail->AddAddress($receiverEmail, $receiverName);//send a copy to the client
    $mail->Subject = $mySubject;
    $mail->WordWrap = 80;
    $mail->AddEmbeddedImage('x.png', 'csvimg', 'csv.png'); // attach file logo.jpg, and later link to it using identfier logoimg
    //$mail->AddEmbeddedImage($serverRoot.'/images/'.$stationeryPrefix.'Header.jpg', $stationeryPrefix.'Header', $stationeryPrefix.'Header.jpg');
    //$mail->AddEmbeddedImage($serverRoot.'/images/'.$stationeryPrefix.'Footer.jpg', $stationeryPrefix.'Footer', $stationeryPrefix.'Footer.jpg');
    //$headerImage = $serverRoot.'/images/'.$stationeryPrefix.'Header.jpg';
    $mail->Body = $stationeryHeader;
    $mail->Body .= $myMessage;
    $mail->Body .= $stationeryFooter;
    if(!$mail->Send()){
        echo '<br />Message FAILED to '.$receiverEmail;
        '.$reportArray[1],$company,$defaultEmail);
    }else{
        echo '<br />Message successfully sent to '.$receiverEmail;
    }//end if mail>send
}//end function sendMyEmail


?>

推荐阅读