title: PHP - Displaying SweetAlert modal popup upon successful registration (AJAX)
body: <p>I'm wanting to make it so that when my ajax code returns no errors, I get the modal. All my code is working perfectly, I just need to know how to make the modal popup once there are no errors. I have a ajax success: function but I'm not sure how this piece of code would work because at the moment, it only works when a button is clicked. </p>
  
  <p>I'm using the SweetAlert JS files</p>
  
  <p><strong>My code :</strong></p>
  
  <p><strong><em>The working modal when button is clicked:</em></strong></p>
  
  <pre><code>  &lt;script&gt;
    !function(e){ "use strict";
     var t = function(){};
     t.prototype.init = function() {
        e("#sa-success").click(function() {
          swal("Registration Successful","Your registration was successful! Check your email and click on your activation link to be able to access your account.","success")
        }
          )
    },e.SweetAlert=new t,e.SweetAlert.Constructor=t}(window.jQuery),function(e){"use strict";e.SweetAlert.init()}(window.jQuery);
  &lt;/script&gt;
  </code></pre>
  
  <p><strong><em>My AJAX registration code I want to make the modal popup once msg = ' ' :</em></strong></p>
  
  <pre><code>    &lt;script&gt;
          $(document).ready(function() {
  
      jQuery.validator.addMethod("noSpace", function(value, element) { 
       return value.indexOf(" ") &lt; 0 &amp;&amp; value != ""; 
    }, "Spaces are not allowed");
  
              $("#registered_form").submit(function() {
  
                  if ($("#registered_form").valid()) {
                      var data1 = $('#registered_form').serialize();
                      $.ajax({
                          type: "POST",
                          url: "inc/pgs/register.php",
                          data: data1,
                          success: function(msg) {
                              console.log(msg);
                              if(msg == '') {
                                  //setTimeout("window.location.href='login.php';",4000);
                              } else {
                                  $("#result").html('&lt;div class="alert alert-danger"&gt;&lt;button type="button" class="close"&gt;&lt;/button&gt;' + msg +'&lt;/div&gt;').slideDown(100);
                                  window.setTimeout(function()
                                  {
                                      $(".alert").fadeTo(500, 0).slideUp(500, function(){
                                          $(this).remove(); 
                                      });
                                  }, 3000);
                              }
                          }
                      });
                  }
                  return false;
              });
          });
      &lt;/script&gt;
  </code></pre>