0


我是 cakephp 的新手。
我需要知道是否有办法让我的应用用户选择发送通知电子邮件或不发送消息。

我已经用谷歌搜索了它,但我没有找到答案......听起来很奇怪,因为这种方法几乎在每个应用程序中都使用......

就像是:

    $this->setFlash('Do you wish to send a notification email?', //yes or no);

    //and if the choice is 'Yes'
    $this->sendEmail();

谢谢!

4

1 回答 1

1

据我了解,蛋糕中的闪光用于通知目的。
但是,我已经设法使用 javascript 做到这一点(或类似的东西)。

从视图中,您可以添加一个在 flash div 上添加“是或否”按钮的函数(使用 firebug 或类似的东西检查其 id),并将这些按钮绑定到实际发送消息的 ajax 调用。

就像是

<script>
$(function() {
    $('#div_of_the_flash_msg'.append('<div id="yes-button">Yes</div> or 
                                      <div id="no-button">No</div>));

    $('#yes-button, #no-button').on('click', function() {
        $.ajax(/*parameters for the ajax call to the controller with the $this->sendEmail() action*/);
     });
});
</script>

这就是我为这些案例所做的,但我不能 100% 确定这是最好的方法......

它也可以完成(也许,从未做过),创建一种新的 Flash 消息,如flash_send_email类,并且在该构造函数中已经定义了按钮及其各自的 javascript,所以当在上面设置 Flash 消息时就像

$this->Session->setFlash('Do you wish to send a notification email?', 'flash_send_email');

并且 flash_send_email.ctp (在 Views 中的 Elements 文件夹内)应该类似于

<div class="your-class">
    <?php echo $message //that's the message you send by parameter on the controller?>
    <div id="yes-button">Yes</div> or <div id="no-button">No</div>
</div>
<script>
      //same script as above to handle the click situations
</script>

同样,第二种方法我没有尝试过,但如果你想在多个视图中显示这种消息,基本上是相同的,只是更可重用。

说了这么多,我仍然认为 Flash 消息应该仅用于通知目的,与用户的任何其他交互都可以通过自定义 div、弹出窗口等进行。考虑一下,也许使用 Flash 并不是绝对必要的这个。

于 2013-04-18T15:01:03.727 回答