title: ajax: force utf-8 encoding
body: <p>I have a webpage in UTF-8, defined in head</p>
  
  <pre><code>&lt;meta charset="utf-8" /&gt;
  </code></pre>
  
  <p>Then I have the external .js file, containing the following code:</p>
  
  <pre><code>function ajax_db(){
      $(document).ready(function(){
  //some variables defined
  
          $.ajax({
              type:"POST",
              url:"db.php",
              data:"u="+uname+"&amp;p="+pass+"&amp;m=1",
              dataType:"text",
              contentType: "application/x-www-form-urlencoded;charset=utf-8",
              success: function(reply){
                  $("#regstat").html("&lt;p class='status'&gt;"+reply+"&lt;/p&gt;");
              }
          });
      });
  }
  </code></pre>
  
  <p>As you see, request goes to db.php, and if <strong>m</strong> set to 1 and not all the fields are filled in, it ends here:</p>
  
  <pre><code>$mode = htmlspecialchars($_POST['m']);
  
  //Mode: 1 - register, 0 - log in, 2 - log out
  
  if ($mode == 1){
  
  if (empty($_POST['u']) || empty($_POST['p']) || empty($_POST['email'])){
      $reply = "Словенъска";
      echo utf8_encode($reply);
      exit;
  }
  //more code
  }
  </code></pre>
  
  <p>Problem is, it's returning some mojibake instead of text. I tried to force utf-8 encoding wherever I could, I put in db.php the following lines:</p>
  
  <pre><code>mb_internal_encoding( 'UTF-8' );
  
  header("Content-type: text/html; charset=utf-8");
  </code></pre>
  
  <p>No effect at all. Furthermore, if I remove the utf8_encode() function, question-mark boxes are returned, which should indicate that no utf-8 is used.</p>
  
  <p>Where could the problem be?</p>
  
  <p>Thanks for your time.</p>