title: onreadystatechange readystate=1 but not 2, 3 or 4
body: <p>This is my first request for help at stackoverflow, so, please, be gentle.</p>
  
  <p>I've searched stackoverflow for questions on this, but haven't found anything that addresses my issue in a way that has allowed me to solve my problem.</p>
  
  <p>I'm trying to use php to send an email containing a user's password from a webpage.  I'm using Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/6.2.7 Safari/537.85.16 as my browser--but I need it work from any major modern browser.</p>
  
  <p>Now <a href="https://pie.gd/test/script-link-events/" rel="nofollow">https://pie.gd/test/script-link-events/</a> seems to indicate that is not supported by my browser, nor by <em>any</em> Mac browser, but that doesn't seem to be consistent with my experience--none of my browser's consoles through an error with my use of <code>onreadystatechange</code>.</p>
  
  <p>The javascript code I'm using is:</p>
  
  <pre><code>    function forgotEmail(str_code)
      {
          bool_debug = &lt;?php echo $bool_debug ?&gt;;
  
          if (bool_debug) { alert("php/forgotEmail.php?"+str_code+" is being called."); } 
  
          if (window.XMLHttpRequest) 
          {
              // code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp = new XMLHttpRequest();
          } // if (window.XMLHttpRequest)
          else 
          {
              // code for IE6, IE5
              xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
          } // if (window.XMLHttpRequest) else
  
          xmlhttp.onreadystatechange = function() 
          {
              alert("readyState = " + xmlhttp.readyState + "\nstatus=" + xmlhttp.status)
  
              if (xmlhttp.readyState==4) 
              {
                  alert("readyState = " + xmlhttp.readyState + "\nstatus=" + xmlhttp.status)
                  if (xmlhttp.status==200)
                  {
                      alert("An email with your password has been sent to the account you gave with your CO Gold submission.  It may take a few minutes to arrive.  Please also check your spam folder.");
                  } // if (xmlhttp.status==200)
              } // if ((xmlhttp.readyState==4) &amp;&amp; (xmlhttp.status==200))
              else
              {
                  alert("There was a problem sending the email with your password.  Please click 'Forgot Password' again.  If you get this alert a third time, please email...");
              } // if ((xmlhttp.readyState==4) &amp;&amp; (xmlhttp.status==200)) else
          } // xmlhttp.onreadystatechange = function() 
  
          xmlhttp.open("GET", "php/forgotEmail.php?"+str_code);
  
      } // function forgotEmail(str_code)
  </code></pre>
  
  <p>and the relevant code from the php page being called by <code>AJAX</code> is:</p>
  
  <pre><code>…
  $headers = 'From: &lt;a valid email address&gt;'."\r\n";
  // the message
  $str_body = $row_psswd['NameFirst'].",\n\n"
      .'Your password is:  '.$row_psswd['psswd']."\n\n";
  
  // use wordwrap() if lines are longer than 70 characters
  $str_body = wordwrap($str_body,90);
  
  // send email
  $return = mail($row_psswd['Email'], "Forgotten Password", $str_body, $headers); 
  
  echo str_replace("\n", "&lt;br /&gt;\n", $str_body);
  
  echo "&lt;br /&gt;\n&lt;br /&gt;\n";
  echo (($return) ? 'email sent' :  'email not sent');
  </code></pre>
  
  <p>Now, when I call the php page by hand, the email gets sent no problem.</p>
  
  <p>But when I click on the HTML button in the webpage that calls the javascript, no email gets sent.  I know the <code>php</code> page is being called correctly, because I get:</p>
  
  <p><code>php/forgotEmail.php?code=JIxRIt is being called.</code></p>
  
  <p>from my first debug <code>alert</code> in the javascript code.  But I only get one <code>alert</code> from within the <code>xmlhttp.onreadystatechange = function()</code>:</p>
  
  <p><code>readyState = 1
  status=0</code></p>
  
  <p>and then the "things haven't worked" <code>alert</code>:</p>
  
  <p><code>There was a problem sending the email with your password.  Please click 'Forgot Password' again.  If you get this alert a third time, please email…</code></p>
  
  <p>So it would appear that <code>readyState</code> is never taking on the values 2, 3 or 4--not taking on the value 4 indicates that the email isn't getting sent, right?</p>
  
  <p>But the fact that that I'm getting an </p>
  
  <p><code>readyState = 1
  status=0</code></p>
  
  <p>makes me think that <code>onreadystatechange</code> is, in fact, supported by Safari (for the Mac).</p>
  
  <p>I hope I've stated the issue clearly and that somebody will be able to help me with this as it's frustrating the @$^&amp;$%@ out of me.</p>