1

当我尝试将西里尔文(俄语)字符发送到数据库(CP 1251 col)时遇到问题。我尝试发送:“Фамилия, Имя”,但在数据库中显示为“Фамилия, РРјСЏ”。有趣的是,当我在没有 Ajax 的情况下发送相同的数据(相同的表单,但请求很简单)时,它可以正常工作。所以,这不是数据库连接问题等等......问题出在我的 Ajax 脚本中。我希望你能帮忙。谢谢。

这是我的代码:

JS文件:

$(function() {
  $('.error').hide();
  function emailCorect(adresaEmail) {
    var shablon = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
    return shablon.test(adresaEmail);
  };

  $("#submit_btn").click(function() {
    // validate and process form
    // first hide any error messages
    $('.error').hide();
    var name = $("input#name").val();
    var email = $("input#email").val();
    var txt = $("#txt").val();
    if(name == "" || email == "" || !emailCorect(email) || txt == "") {
      if (name == "") {
        $("#name_error").show();
        $("input#name").focus();
      }
      if (email == "") {
        $("#email_error").show();
        $("input#email").focus();
      }
      if(!emailCorect(email)) {
        // in case it's not a valid e-mail-ul
        $("#email_error").show();
        $("input#email").focus();
      }
      if (txt == "") {
        $("#txt_error").show();
        $("#txt").focus();
      }
      return false;
    }
    var dataString = 'name='+ name + '&email=' + email + '&txt=' + txt;
    $.ajax({
      type: "POST",
      url: "./../ajax/opinii.php",
      data: dataString,
      success: function() {
        $('#adauga').html("<div id='ajax_succes_txt'></div>");
        $('#ajax_succes_txt').fadeIn(1500, function() {
          $('#ajax_succes_txt').html("<img src='../juststy/images/check.png' /><br />").append("Opinia dvs. a fost publicat&#259;.<br /><span style='font-size: 10px; color:red;'><b>aten&#355;ie:</b> dac&#259; vei publica opinii &icirc;ntr-un interval scurt de timp, acestea nu vor fi publicate.");
        });
        $('#render').load("./../inc/1/");
      }
    });
    return false;
  });
});

PHP 文件:

<?php
  require_once("./../core/system.php");
  dbconnect();
  $name = $_POST['name'];
  $email = $_POST['email'];
  $txt = $_POST['txt'];
  $query = mysql_query("INSERT INTO opinii(name,mail,txt,ip,data) VALUES(
    ".sqlesc($name).",".sqlesc($email).",".sqlesc($txt).",".
    sqlesc($_SERVER['REMOTE_ADDR']).",".sqlesc(get_time()).")")
    or die(mysql_error());
  if($query){echo "OK"; }?>

PHP 连接函数(添加于 13:15 15-07-2012)

function dbconnect($mode = "load")
{
    global $mysql_host, $mysql_user, $mysql_pass, $mysql_db;

    $link = @mysql_connect($mysql_host, $mysql_user, $mysql_pass);

    if (!@mysql_connect($mysql_host, $mysql_user, $mysql_pass)) 
    {
        die('A intervenit o eroare la conectarea cu baza de date. Dac&#259; e&#351;ti un utilizator, revino mai t&acirc;rziu.');
    }

    mysql_select_db($mysql_db) or die('A intervenit o eroare la selectarea bazei de date. Dac&#259; e&#351;ti un utilizator, revino mai t&acirc;rziu.');

    switch($mode) {
        case "load":    
            mysql_query("SET NAMES cp1251");
            break;
        case "insert":
            mysql_query("SET NAMES utf8");
            break;
    }

}
4

0 回答 0