I'm new in cakephp.
i need to know if there's a way to let my app users to choose to send a notification email or not with a flash message.
I already googled for it but I didn't find an answer... sounds strange because this method is used nearly in every application...
something like:
$this->setFlash('Do you wish to send a notification email?', //yes or no);
//and if the choice is 'Yes'
$this->sendEmail();
Thanks!
For what I understand, the flash in cake is just for notification purposes.
But, I have manage to do that (or something similar) using javascript.
From the view you could add a function that adds "yes or no" buttons on the flash div (check out its id with firebug or something like that), and bind those buttons to an ajax call that actually sends the message.
Something like
<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>
This is what I've done for those cases, but I can't be 100% sure it's the best way...
It could also be done (maybe, never done it), creating a new kind of flash message, like flash_send_email
class, and in that constructor have the buttons already defined, with its respective javascript, so when setting the flash message above it would be like
$this->Session->setFlash('Do you wish to send a notification email?', 'flash_send_email');
and the flash_send_email.ctp (inside Elements folder in Views) should be something like
<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>
Again, the second approach I haven't tried, but is basically the same thing just a bit more reusable if you want to have that kind of message in more than one view.
All that been said, I still believe flash messages should be use just for notification purposes, any other interaction with the user can be made by custom divs, popups, etc etc. Give it a thought, maybe it's not absolutely necessary to use flash for this.