Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery post将错误的输入值传递给PHP_Javascript_Php_Jquery - Fatal编程技术网

Javascript jQuery post将错误的输入值传递给PHP

Javascript jQuery post将错误的输入值传递给PHP,javascript,php,jquery,Javascript,Php,Jquery,除了一件事,一切都很好。当我在输入框中写东西时,首先它通过并被正确地回显了一会儿,然后最后几个字符/数字消失了。 例如,如果我正在写作: 你好 PHP回显Hello,然后,过了一会儿,单词变成H或Hell 消失字符的数量取决于键入单词的速度,就像我写得快一样,就像字符消失一样。 当我写得很慢时,一个字母接一个字母,单词被正确地回响,没有任何变化。 以下是代码供参考: HTML: <input name="paramA" id="paramA" type="text" class="form

除了一件事,一切都很好。当我在输入框中写东西时,首先它通过并被正确地回显了一会儿,然后最后几个字符/数字消失了。 例如,如果我正在写作: 你好 PHP回显Hello,然后,过了一会儿,单词变成H或Hell 消失字符的数量取决于键入单词的速度,就像我写得快一样,就像字符消失一样。 当我写得很慢时,一个字母接一个字母,单词被正确地回响,没有任何变化。 以下是代码供参考:

HTML:

<input name="paramA" id="paramA" type="text" class="form-control" maxlength="5" required/>
PHP:


任何帮助都将不胜感激。

这应该能帮到你

var to = null;
$("#paramA").on("keyup", function () {        
    var paramB = $('#paramB').val();
    var paramA = $.trim($(this).val());
    if ( paramA.length > 0) { // adjust 0 to a minimum number of characters before post to php
        if (to !== null) {
           clearTimeout(to);
        }
        to = setTimeout(function() {
            $.post( 'http://localhost/folder/code.php',    
                {'paramB':paramB,'paramA':paramA}, 
                function(data) {
                    $("#result").html(data);
                }
            );
        }, 500); // adjust the time in ms, how long you want to wait until the post will be send
    }
}); 

这应该对你有用

var to = null;
$("#paramA").on("keyup", function () {        
    var paramB = $('#paramB').val();
    var paramA = $.trim($(this).val());
    if ( paramA.length > 0) { // adjust 0 to a minimum number of characters before post to php
        if (to !== null) {
           clearTimeout(to);
        }
        to = setTimeout(function() {
            $.post( 'http://localhost/folder/code.php',    
                {'paramB':paramB,'paramA':paramA}, 
                function(data) {
                    $("#result").html(data);
                }
            );
        }, 500); // adjust the time in ms, how long you want to wait until the post will be send
    }
}); 

问题是,ajax调用并不总是花费相同的时间。
因此,如果你打字很快,对“Hell”和“Hello”的呼叫几乎同时开始

因此,如果出于某种原因,“Hell”调用需要更长的时间,PHP将返回“Hello”回显,然后您将收到“Hell”。(覆盖输出中的“Hello”。)

解决这一问题的办法是

或者在
keyup
函数中使用
timeout
仅在500毫秒内没有键入任何内容时启动ajax调用。
(您仍然应该取消任何挂起的ajax调用,以确保)

像这样:

var timeout = null;
$("#paramA").on("keyup", function () {
    clearTimeout(timeout); // clear currently waiting timeout
    timeout = setTimeout(function(){ // wait 500 ms before ajax call
        var paramB = $('#paramB').val();
        var paramA = $('#paramA').val();
        var spaced = $.trim($(this).val());
        if ( !$(this).val() || $(this).val() !== spaced) {
            $("#result").html("");
        } else {
            $.post( 'http://localhost/folder/code.php',    
                    {'paramB':paramB,'paramA':paramA}, 
                    function(data) {
                        $("#result").html(data);
                    }
            );
        }
    }, 500);
}); 

问题是,ajax调用并不总是花费相同的时间。
因此,如果你打字很快,对“Hell”和“Hello”的呼叫几乎同时开始

因此,如果出于某种原因,“Hell”调用需要更长的时间,PHP将返回“Hello”回显,然后您将收到“Hell”。(覆盖输出中的“Hello”。)

解决这一问题的办法是

或者在
keyup
函数中使用
timeout
仅在500毫秒内没有键入任何内容时启动ajax调用。
(您仍然应该取消任何挂起的ajax调用,以确保)

像这样:

var timeout = null;
$("#paramA").on("keyup", function () {
    clearTimeout(timeout); // clear currently waiting timeout
    timeout = setTimeout(function(){ // wait 500 ms before ajax call
        var paramB = $('#paramB').val();
        var paramA = $('#paramA').val();
        var spaced = $.trim($(this).val());
        if ( !$(this).val() || $(this).val() !== spaced) {
            $("#result").html("");
        } else {
            $.post( 'http://localhost/folder/code.php',    
                    {'paramB':paramB,'paramA':paramA}, 
                    function(data) {
                        $("#result").html(data);
                    }
            );
        }
    }, 500);
}); 


您想用此行存档什么?->if(!$(this).val()| |$(this).val()!==spooted){检查输入是否为空,以便JavaScript可以将其传递给PHP进行处理。改为:if(spooted.length>0){将代码移到此处…},没有其他需要的好建议,谢谢。我试图编辑这个问题,但我做不到。安迪在我之前编辑了它。无论如何,我以后会尝试编辑它。我已经添加了一个答案,它应该可以帮助你得到你想要的。你想用这一行存档什么?->if(!$(this.val()|;$(this.val()!==间隔){它检查输入是否为空,因此JavaScript可以将其传递给PHP进行处理,没有其他需要的好建议,谢谢。我试图编辑这个问题,但我做不到。安迪在我之前编辑了它。无论如何,我以后会尝试编辑它。我添加了一个答案,这应该会帮助你,以得到你想要的。你改进了代码,疯狂地在发布之前检查字符数。然而,主要问题没有解决,paSSD值仍然与输入框中的实际值不同。抱歉,我想你已经知道了。我将更改我的答案以面对计时问题…谢谢你,奥利弗,我很感激。你改进了代码,并在发布之前检查了字符数。但是主要问题没有解决,传递的值仍然与输入框中的实际值。对不起,我想你已经知道了。我会改变我的答案来面对时间问题…谢谢你,奥利弗,我很感激。谢谢你,安迪,谢谢你的书和对这个问题的解释。我认为这是理想的解决方案。但是我自己的代码有点问题,我一直在尝试,看看是什么原因使它在内部不起作用脚本的这部分。timeout=setTimeout(function(){},500)安迪,再次感谢你,最后它对我很好,我只需要在StEffice函数之前移动StimeTimeOf函数。谢谢你的安迪和你的问题的解释。我认为这是一个理想的解决方案。但是,我自己的代码有点问题,我一直在努力让它在这个范围内不起作用。t的脚本。timeout=setTimeout(function(){},500);Andy,再次感谢您,最后它与我一起工作得很好,我只需要将setTimeout函数移到Post函数之前。