Wednesday, October 22, 2014

Jquery-Chosen-Knockout in a Dialog or Window Not Working?

If you are loading a Chosen element in a pop-up dialog box or window, you may find that the "multiple" types or "multiselect" type of element does NOT display the selectedOptions that you specified on the data-bind. You need to trigger an update by passing a reference of window to the open function.
This was the template that was loaded into the dialog window (located in a .cshtml file):
  1. <script type="text/html" id="junk-template">
  2. <div>
  3. <!--set tabindex of dropdown to prevent from auto opening when editing-->
  4. <select id="myItems" tabindex="-1" data-placeholder=" " data-bind="chosen: MyOptions, optionsText: 'Text', optionsValue: 'Value', selectedOptions: MySelectedOptions" multiple="multiple"></select>
  5. </div>
  6. </script>
This is the code in the Javascript file to load, data-bind and open the window. NOTICE 2 things, you must use the z-index and pass the window by reference to the open function. In order to resolve issues with chosen in a popup window the z-index must be set.
  1. function _openMyWindow(myOptions) {
  2. var selectedOptions = _getOptions();
  3. $window = $("<div/>");
  4. $window.append($('#junk-template').html());
  5. if (!myVM) {
  6. myVM = {
  7. MyOptions: ko.observableArray(myOptions),
  8. MySelectedOptions: ko.observableArray(selectedOptions != undefined ? selectedOptions : [])
  9. }
  10. }
  11. ko.applyBindings(myVM, $window[0]);
  12. $window.Window({
  13. width: '450px', height: 'auto', modal: true, zIndex: 998, maximizable: false, open: _openWindow($window), title: "Junk Title"
  14. });
  15. }
Now, pay attention here, see I am using the window reference to trigger the chosen element update!
  1. function _openWindow(container) {
  2. var element = $(container).find('#myItems');
  3. if (element != null) {
  4. element.trigger("chosen:updated");
  5. }
  6. }
Hope you could follow that and I hope it helps!

Saturday, October 18, 2014

PHP SMTP GoDaddy Email Alternative: SendGrid

If you have ever worked with GoDaddy's SMTP relay email service, you will find it sometimes just quits working. I know GoDaddy is not about being a reliable company known for quality so it very silly of me to complain. However, I kind of need to send out emails from a website as needed, NOT just when GoDaddy thinks it is needed.
I finally was fed up with this terrible service. And Gmail, which I was trying to use as an alternative, just up and deleted my email account for whatever reason, so I searched out an alternative. I found SendGrid. For a basic 'FREE' account, I can send up to 200 emails per day. Which is definitely good enough for me - I am not sending spam, I am sending out an occasional newsletter to a subscribed membership!
For PHP, you need at least 5.3, I believe. You can download their code package to use or replace your SMTP credentials.
Here is a sample using their code package:
  1. include('sendgrid-php/sendgrid-php.php');
  2. function sendEmailSendGrid($subject, $message, $to, $fileName){
  3.         
  4.      $sendgrid = new SendGrid('myusername', 'mypassword');
  5.      $to = trim($to);
  6.      $htmlMessage = "<html><body>".$message."</body></html>";
  7.      $from='junk@junk.com';
  8.     
  9.      $email = new SendGrid\Email();
  10.      $email->addTo($to)->
  11. setFrom($from)->
  12. setSubject($subject)->
  13. setText($message)->
  14. setHtml($htmlMessage);
  15. if($fileName != "")
  16.     $email->addAttachment($fileName);
  17.     
  18. return $sendgrid->send($email);
  19.     
  20. }
Otherwise replace your SMTP stuff like this:
  1. function sendEmailSendGrid($subject, $message, $to, $rid, $nid){
  2.     
  3.      $to = trim($to);
  4.     
  5.      $htmlMessage = "<html><body>".$message."</body></html>";
  6.     
  7.      $host = "smtp.sendgrid.net";
  8. $port = "587";
  9. $username = "username";
  10. $password = "password";
  11. $crlf = "\n";
  12.     
  13. $smtp = Mail::factory('smtp',
  14. array ('host' => $host,
  15. 'port' => $port,
  16. 'auth' => true,
  17. 'username' => $username,
  18. 'password' => $password));
  19.         
  20.     $from='junk@junk.com';
  21.                  
  22. $headers = array ('From' => $from,
  23. 'To' => $to,
  24. 'Subject' => $subject,
  25. 'Content-type' => 'text/html; charset=iso-8859-1');
  26. $mail = $smtp->send($to, $headers, $htmlMessage);
  27. if (PEAR::isError($mail)) {
  28. echo("<p>" . $mail->getMessage() . "</p>");
  29. } else {
  30. echo("<p>Message successfully sent to: ".$to."</p>");
  31. }
  32. }