Wrong encoding in AJAX

I know, the question has already been asked, but I did not find a solution, I pass variables, in one variable the Russian text (rName), I overtake it in windows-1251, I display it on the screen – everything is fine. I’m trying to add an error to the database (Unknown column ‘‹єЂѓЂє’ in ‘field list’) everywhere the encoding is windows-1251, there is another script – there is also Russian text, everything works fine. The only difference is the variables from AJAX, in that I pass

`data: "rKod="+rKod+"&rLogin="+rLog...`


data: {rKod:rKod, rEmail:rEmail, rLogin:rLogin, rPass:rPass, rPass2:rPass2, rName:rName}

In this script, for some reason, one variable does not reach in the first way (it doesn’t matter which one, but one does not reach)

    header("Content-type: text/html; charset=windows-1251");
    if ($_SESSION['secpic'] == trim($_POST['rKod'])) {
        $rLogin = trim($_POST['rLogin']);
        $rPass  = trim($_POST['rPass']);
        $rPass2 = trim($_POST['rPass2']);
        $rEmail = trim($_POST['rEmail']);
        $rName  = iconv("utf-8", "windows-1251", $_POST['rName']);
        if ($rLogin == '') {
            die("і і<br />\n");
        } elseif (!preg_match("/^\w{3,}$/", $rLogin)) {
            die("і і і ,  ї  !<br />\n");
        if ($rName == '') {
            die("і І'<br />\n");
        } elseif (!preg_match("/^[--]{3,}$/", $rName)) {
            die("І' і і ,  ї  !<br />\n");
        if ($rEmail == '') {
            die("E-mail  <br />\n");
        } elseif (!preg_match("/^[a-zA-Z0-9_\.\-][email protected]([a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,6}$/", $rEmail)) {
            die("E-mail  і<br />\n");
        if ($rPass == '' || $rPass2 == '') {
            die("  <br />\n");
        } elseif ($rPass !== $rPass2) {
            die("    і<br />\n");
        } elseif (!preg_match("/^\w{3,}$/", $rPass)) {
            die(" є і !<br />\n");
        $mdPassword = md5($rPass);
        $time       = time();
        $inc        = 1;
        mysql_query("SET NAMES 'cp1251'");
        mysql_query("SET character_set_client = cp1251");
        mysql_query("SET collation_connection=cp1251_general_ci");
        mysql_query("SET character_set_results = cp1251");
        echo $rName . '<br>';
        mysql_query("INSERT INTO users (login, pass, email, timestamp, name) VALUES ('$rLogin','$mdPassword','$rEmail',$time, $rName)", $connect);
        echo mysql_error($connect) . '<br>';
        if (mysql_error($connect) != "") {
            die("     ,  <br />\n");

Answer 1, authority 100%

If the data is sent via AJAX, then it will come to the server in UTF-8, use iconvto convert it to the required encoding.

Answer 2

mysql_query("INSERT INTO users (login, pass, email, timestamp, name) VALUES ('$rLogin','$mdPassword','$rEmail',$time, $rName)", $connect);

what …. is $rName without quotes huh???