【问题标题】:Character counter for form inputs- not counting表单输入的字符计数器 - 不计数
【发布时间】:2014-12-03 11:39:58
【问题描述】:

任何人都知道为什么这段代码没有显示剩余的字符并且只显示最大数量的字符,我已经经历了几次我似乎找不到错误。

HTML

<form class="comment" method="post" action="postComment.php">
    <input type="text" placeholder="Name" name="Cname" onKeyUp="charLeft(this.value,30,'n')"><br/><span id="nCharLeft"></span><br/>
    <input type="text" placeholder="Email" name="Cemail" onKeyUp="charLeft(this.value,50,'e')"><br/><span id="eCharLeft"></span><br/>
    <textarea rows="4" placeholder="Please leave a comment." name="Ccomment" onKeyUp="charLeft(this.value,300,'c')"></textarea><br/><span id="cCharLeft"></span><br>
    <input type="submit" value="Post Comment"><br/>
</form>

JavaScript

function charLeft(val,len,indi) {
    var output = indi + "CharLeft";
    if (val.length==0) { 
        return;
    }
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
       document.getElementById(output).innerHTML=xmlhttp.responseText;
    }
}
xmlhttp.open("GET","charLeft.php?q="+val+"&m="+len,true);
xmlhttp.send();
}

PHP (charLeft.php)

<?php
$v = $_REQUEST['v'];
$m = $_REQUEST['m'];
$len = strlen($v);
$charleft = $m - $len;

echo $charleft==="1" ? "$charleft character left." : "$charleft characters left.";
?>

【问题讨论】:

    标签: javascript php html character counter


    【解决方案1】:

    在您的 AJAX 调用中,您在 q 中发送值,但以 $v = $_REQUEST['v'];

    因此,要么将 AJAX 调用更改为 xmlhttp.open("GET","charLeft.php?v="+val+"&m="+len,true);

    或将 PHP 更改为 $v = $_REQUEST['q'];

    【讨论】:

      【解决方案2】:

      你发送了"charLeft.php?q="+val+"&m="+len,但是通过$v = $_REQUEST['v'];得到值

      试试

      $v = $_REQUEST['q'];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-11
        • 1970-01-01
        • 2022-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多