Advertisement
Clowerweb

Twilio-Plain

Sep 12th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // wait until the document is loaded
  2. document.addEventListener('DOMContentLoaded', function() {
  3.   // the front-end sms form
  4.   var form = document.getElementById('sms-form');
  5.  
  6.   // modern browsers
  7.   if(form.addEventListener) {
  8.     form.addEventListener('submit', process_form, false);
  9.   // old IE
  10.   } else if(form.attachEvent) {
  11.     form.attachEvent('onsubmit', process_form);
  12.   }
  13.  
  14.   // validate the form, format the data and send the ajax post
  15.   function process_form(e) {
  16.     // div which shows success message or error message(s)
  17.     var $statusEl = document.getElementById('sms-status');
  18.     // the phone number entered by the user
  19.     var number    = document.getElementById('sms-number').value;
  20.     // the message entered by the user
  21.     var message   = document.getElementById('sms-message').value;
  22.     // craft csrf token
  23.     var csrf      = document.getElementsByName('CRAFT_CSRF_TOKEN')[0].value;
  24.     // hidden action field (default: twilioSms/Sms/sendSms)
  25.     var action    = document.getElementsByName('action')[0].value;
  26.  
  27.     // prevent form submit
  28.     e.preventDefault();
  29.  
  30.     // disable the submit button to prevent double sending
  31.     document.getElementById('sms-submit').disabled = true;
  32.  
  33.     // create a new ajax instance
  34.     var http = new XMLHttpRequest();
  35.     // send to /actions/text-it3/textit/send-sms
  36.     var url  = '/actions/text-it3/textit/send-sms';
  37.     // querystring to send
  38.     var data = '';
  39.  
  40.     // encode all our POST data because it gets sent as a querystring
  41.     data += 'CRAFT_CSRF_TOKEN=' + encodeURIComponent(csrf);    // craft csrf token
  42.     data += '&action='          + encodeURIComponent(action);  // action (default: twilioSms/Sms/sendSms)
  43.     data += '&sms_user_phone='  + encodeURIComponent(number);  // phone number entered by the user
  44.     data += '&sms_message='     + encodeURIComponent(message); // message entered by the user
  45.  
  46.     // prepare ajax post
  47.     http.open('POST', url, true);
  48.     // send a header saying this is url encoded form data
  49.     http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  50.  
  51.     // call anon function when the state changes
  52.     http.onreadystatechange = function() {
  53.       // the post was sent, re-enable the submit button
  54.       document.getElementById('sms-submit').disabled = false;
  55.  
  56.       // make sure the status was a success
  57.       if(http.readyState === 4 && http.status === 200) {
  58.         // parse the response as json
  59.         var res = JSON.parse(http.responseText);
  60.  
  61.         // if the message sent successfully
  62.         if(res.success) {
  63.           // reset the form
  64.           form.reset();
  65.           // display the success message
  66.           $statusEl.innerHTML = res.msg;
  67.           $statusEl.style.display = 'block';
  68.         // the back end processor returned errors
  69.         } else {
  70.           // display the error message(s)
  71.           var status = '<strong>The following errors occurred:</strong><br />';
  72.           status += '<ul>';
  73.  
  74.           res.errors.forEach(function(error) {
  75.             status += '<li>' + error + '</li>';
  76.           });
  77.  
  78.           status += '</ul>';
  79.  
  80.           // display errors
  81.           $statusEl.innerHTML = status;
  82.           $statusEl.style.display = 'block';
  83.         }
  84.       }
  85.     } // end http.onreadystatechange
  86.  
  87.     // send the post
  88.     http.send(data);
  89.   } // end function process_form()
  90. }); // end DOMContentLoaded
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement