任何人都有任何见解为什么这段代码没有显示剩下的字符,只显示最大字符数,我已经经历了好几次,我似乎无法找到错误。
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.";
?>
答案 0 :(得分:1)
在您的AJAX调用中,您正在q中发送值但是将其作为 $ v = $ _REQUEST [&#39; v&#39;];
因此,要么将AJAX调用更改为 xmlhttp.open(&#34; GET&#34;&#34; charLeft.php V =&#34; + VAL +&#34;&安培; M =&#34 + LEN,TRUE);
或将PHP更改为 $ v = $ _REQUEST [&#39; q&#39;];
答案 1 :(得分:0)
您发送了“charLeft.php?q =”+ val +“&amp; m =”+ len,但是通过$ v = $ _REQUEST ['v']获取值;
尝试
$v = $_REQUEST['q'];