mb_strlen() & strlen() don't return correct values from an Ajax call to PHP

前端 未结 2 1489
深忆病人
深忆病人 2021-01-14 02:17

How can I add a check in the PHP for the length of the $username passed. The site is UTF-8 but I believe Javascript is using a different encoding. You can see in the comm

相关标签:
2条回答
  • 2021-01-14 02:59

    PHP is a byte processor, it is not charset-aware. That has a number of tricky consequences.

    Strlen() returns the length in bytes, not the length in characters. This is because php's "string" type is actually an array of bytes. Utf8 uses more than one byte per character for the 'special characters'. Therefore strlen() will only give you the right answer for a narrow subset of text (= plain english text).

    Mb_strlen() treats the string as actual characters, but assumes it's in the encoding specified via mbstring.internal_encoding, because the string itself is just an array of bytes and does not have metadata specifying its character set. If you are working with utf8 data and set internal_encoding to utf8 it will give you the right answer. If your data is not utf8 it will give you the wrong answer.

    Mysql will receive a stream of bytes from php, and will parse it based on the database session's character set, which you set via the SET NAMES directive. Everytime you connect to the database you must inform it what encoding your php strings are in.

    The browser receives a stream of bytes from php, and will parse it based on the content-type charset http header, which you control via php.ini default_charset. The ajax call will submit in the same encoding as the page it runs from.

    Summarized, you can find advice on the following page on how to ensure all your data is treated as utf8. Follow it and your problem should resolve itself. http://malevolent.com/weblog/archive/2007/03/12/unicode-utf8-php-mysql/

    0 讨论(0)
  • 2021-01-14 03:01

    From a quick glance, you can clean this up:

    if (request.status == 200) {
        if (request.responseText == "available") {
            document.getElementById("txt_username").innerHTML = "NAME AVAILABLE!";
            document.images['img_username'].src=img_smile;
            document.getElementById("continue").disabled = false;
            document.getElementById("field_username").className = 'validated';
        } else if (request.responseText == "taken") {
            document.getElementById("txt_username").innerHTML = "NAME TAKEN!";
            document.images['img_username'].src=img_sad;
            document.getElementById("field_username").className = 'error';
        } else if (request.responseText == "invalid_too_short") {
            document.getElementById("txt_username").innerHTML = "TOO SHORT!";
            document.images['img_username'].src=img_sad;
            document.getElementById("field_username").className = 'error';
        } else {
            document.getElementById("txt_username").innerHTML = "AJAX ERROR!";
                    document.images['img_username'].src=img_sad;
            document.getElementById("field_username").className = 'error';
        }
      }
    

    to:

    // I prefer triple equals
    // Read more at http://javascript.crockford.com/style2.html
    if (request.status === 200) {
            // use variables!
            var txtUsername = document.getElementById('txt_username');
            var fieldUsername = document.getElementById('field_username');
            var imgUsername = document.getElementById('img_username');
    
            var response = request.responseText;
    
            var error = true;
    
            // you can do a switch statement here too, if you prefer
            if (response === "available") {
                txtUsername.innerHTML = "NAME AVAILABLE!";
    
                document.getElementById("continue").disabled = false;
    
                error = false;
    
            } else if (response === "taken") {
                txtUsername.innerHTML = "NAME TAKEN!";
    
            } else if (response === "invalid_too_short") {
                txtUsername.innerHTML = "TOO SHORT!";
    
            } else {
                txtUsername.innerHTML = "AJAX ERROR!";
            }
    
            // refactor error actions
            if (error) {
                imgUsername.src = img_sad;
                fieldUsername.className = 'error';
            } else {
                imgUsername.src = img_smile;
                fieldUsername.className = 'validated';
            }
    }
    
    0 讨论(0)
提交回复
热议问题