我的 jQuery 和 PHP 对同一件事给出不同的结果?

发布于 2024-09-04 07:03:47 字数 1440 浏览 14 评论 0原文

烦人的大脑麻木问题。

我有两个函数来检查字符串的长度(主要是 js 也被截断),这是 Javascript 中的一个:

$('textarea#itemdescription').keyup(function() {
    var charLength = $(this).val().length;
    // Displays count
    $('span#charCount').css({'color':'#666'});
    $('span#charCount').html(255 - charLength);

    if($(this).val().length >= 240){
        $('span#charCount').css({'color':'#FF0000'});
    }
    // Alerts when 250 characters is reached
    if($(this).val().length >= 255){
        $('span#charCount').css({'color':'#FF0000'});
        $('span#charCount').html('<strong>0</strong>');

        var text = $('textarea#itemdescription').val().substring(0,255)
        $('textarea#itemdescription').val(text);
    }
});

这是我的 PHP 来仔细检查:

if(strlen($_POST["description"])>255){
    echo "Description must be less than ".strlen($_POST["description"])." characters";
    exit();
}   

我正在使用 jQuery Ajax 来发布文本区域中的值。然而我的 php 验证说 strlen() 比我的 js 本质上说的要长。例如,如果我输入一个实心字符串,它会显示 0 或 3 个字符,直到 255。然后我单击“保存”,php 给出的长度为 261。

有什么想法吗?

这是否与 js 读取不同或遗漏的特殊字符、位大小有关?还是与其他事情有关?也许今天病了!...:P

更新: 我添加了 var_dump($_POST['description']) 查看传递的内容并且返回转义斜线,例如发生了什么?我尝试添加 stripslashes();无济于事...他们从哪里来?

更新 2 - 问题已解决

基本上我想我刚刚意识到我的服务器打开了魔术引号... grr 所以我现在在处理之前已经去掉了斜​​杠。有点烦人,但它必须做!

感谢您的帮助!

谢谢, 斯特凡

Annoying brain numbing problem.

I have two functions to check the length of a string (primarily, the js one truncates as well) heres the one in Javascript:

$('textarea#itemdescription').keyup(function() {
    var charLength = $(this).val().length;
    // Displays count
    $('span#charCount').css({'color':'#666'});
    $('span#charCount').html(255 - charLength);

    if($(this).val().length >= 240){
        $('span#charCount').css({'color':'#FF0000'});
    }
    // Alerts when 250 characters is reached
    if($(this).val().length >= 255){
        $('span#charCount').css({'color':'#FF0000'});
        $('span#charCount').html('<strong>0</strong>');

        var text = $('textarea#itemdescription').val().substring(0,255)
        $('textarea#itemdescription').val(text);
    }
});

And here is my PHP to double check:

if(strlen($_POST["description"])>255){
    echo "Description must be less than ".strlen($_POST["description"])." characters";
    exit();
}   

I'm using jQuery Ajax to post the values from the textarea. However my php validation says the strlen() is longer than my js is essentially saying. So for example if i type a solid string and it says 0 or 3 chars left till 255. I then click save and the php gives me the length as being 261.

Any ideas?

Is it to do with special characters, bit sizes that js reads differently or misses out? Or is it to do with something else? Maybe its ill today!... :P

Update:
I added var_dump($_POST['description'])
to see what was passed and it was returning escape slashes e.g. what\'s going on? I have tried adding stripslashes(); to no avail... where are they coming from?

UPDATE 2 - PROBLEM SOLVED:

Basically I think I just realised my server has magic quotes turned on... grr
So I have stripped slashes before processing now. Bit annoying but it will have to do!!

Thanks for your help!

Thanks,
Stefan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

乜一 2024-09-11 07:03:47

调试此问题的最简单方法就是从 PHP 脚本中使用:
var_dump($_POST['description']

我建议您还使用浏览器中的查看源代码来查看任何转义代码、特殊字符代码等...

The easiest way to debug this is simply from your PHP script, by using:
var_dump($_POST['description']

I suggest you also use view source in your browser to see any escape code, special char codes, etc...

落墨 2024-09-11 07:03:47

如果您发布更多前端代码,尤其是在执行实际 POST 的地方,将会有所帮助。也就是说,你确定每次都会调用 keyup 吗?如果用户只是将文本粘贴到框中,您是否验证它仍然被调用?

另请记住,JavaScript 不足以保证字符串小于给定长度。用户可以禁用 JavaScript,而精明的“用户”可以发送自己的包含超过 255 个字符的 POST 请求。

It would help if you posted more of your front-end code, especially where you are doing the actual POST. That said, are you sure that keyup is called every time? If the user just pastes text into the box have you verified it is still called?

Also keep in mind that JavaScript is not good enough to guarantee that a string will be less than a given length. A user could disable JavaScript, and a savvy "user" can send their own POST request with more than 255 chars.

不羁少年 2024-09-11 07:03:47

我怀疑很少有字符是换行符(你说你使用文本区域),当你使用 javascript 进行验证时,这些字符会被忽略。

I suspect that few characters are line breaks (you say you use textarea) that are ignored while you validate using javascript.

北方的韩爷 2024-09-11 07:03:47

我发现有两件事可能会导致您的问题。

  • 首先substring(0,255)返回256个字符
  • 其次magic_quotes可能在php.ini中打开,PHP尝试给你转义字符串但没有' 没有一直正确执行操作,

edit

没有重新读取子字符串定义,忽略第一个,但 magic_quotes 可能会检查该子字符串

I see 2 things that might be causing your problem.

  • firstly substring(0,255) returns 256 characters
  • secondly magic_quotes might be turned on in php.ini, PHP tries to give you escaped strings but doesn't do it right all the time

edit

doh didnt re-read the substring definition, ignore the first one but magic_quotes might be on check that one

舟遥客 2024-09-11 07:03:47

如果使用 UTF-8 编码,PHP strlen() 将计算字节数,而不是字符数。如果你有任何非 ASCII 的东西,就会发生这种情况。使用 mb_strlen()。魔术引号也可以添加一些字符。

If you use UTF-8 encoding, PHP strlen() is counting the bytes, not the characters. If you have anything non-ASCII, this will happen. Use mb_strlen(). Magic quotes can add a few characters also.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文