PHP:
if(isset($_POST["username"]) && $_POST["username"] != ""){
$user= $_POST['username'];
if(strlen($user<4)){
echo"username must be 8 charter";
}
}
JS:
function callme() {
var showme = document.getElementById("show");
var user = document.getElementById("uname").value;
//for check new browser show ajax from
if (user !== "") {
showme.innerHTML = '<h1> loading.....</h1>';
var hr = new XMLHttpRequest() {
hr.open("post", "index.php", true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.readystatechange = function () {
if (hr.readystate == 4 && hr.status == 200) {
showme.innerHTML = hr.responseText;
}
}
var v = "username=" + user;
hr.send(v);
};
}
}
HTML:
<span>username:</span>
<input type="text" name="uname" id="uname" onkeyup="callme();"/>
<div id="show"></div>
我的代码无效,并且没有显示任何错误。
callme();
函数是我认为错误的地方,即处理用户名验证。
答案 0 :(得分:1)
在您的代码中,您有SyntaxError。
请在功能正文后添加}。 它正在运作
<?php
if(isset($_POST["username"]) && $_POST["username"] != "")
{
$user= $_POST['username'];
if(strlen($user)<4) /// your mistake in php code
{
echo"username must be 8 charter";
}
}
?>
<script type="text/javascript" language="javascript">
function callme()
{
var showme = document.getElementById("show");
var user = document.getElementById("uname").value;
//for check new browser show ajax from
if(user!=="")
{
showme.innerHTML='<h1> loading.....</h1>';
var hr = new XMLHttpRequest()
{
hr.open("post","index.php",true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.readystatechange=function()
{
if(hr.readystate== 4 && hr.status==200) {
showme.innerHTML= hr.responseText;
}
}
var v="username="+ user;
hr.send(v);
}
}// you missed
</script>
</head>
<body>
<span>username:</span>
<input type="text" name="uname" id="uname" onkeyup="callme();"/>
<div id="show"></div>
</body>
答案 1 :(得分:0)
使用以下代码
<?php
if(isset($_POST["username"]) && $_POST["username"] != "")
{
$user= $_POST['username'];
if(strlen($user<4))
{
echo"username must be 8 charter";
}
}
?>
<script type="text/javascript" language="javascript">
function callme()
{
var showme = document.getElementById("show");
var user = document.getElementById("uname").value;
//for check new browser show ajax from
if(user!=="")
{
showme.innerHTML='<h1> loading.....</h1>';
var hr = new XMLHttpRequest()
{
hr.open("post","index.php",true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.readystatechange=function()
{
if(hr.readystate== 4 && hr.status==200) {
showme.innerHTML= hr.responseText;
}
}
var v="username="+ user;
hr.send(v);
}
}
}// you missed the closing brace.
</script>
</head>
<body>
<span>username:</span>
<input type="text" name="uname" id="uname" onkeyup=callme() />
<div id="show"></div>
</body>`enter code here`
答案 2 :(得分:0)
您的最终代码将为,
PHP:
<?php
if(isset($_POST["username"]) && $_POST["username"] != ""){
$user= $_POST['username'];
if(strlen($user<4)){
echo"username must be 8 charter";
}
}
?>
JS:
function callme() {
var showme = document.getElementById("show");
var user = document.getElementById("uname").value;
//for check new browser show ajax from
if (user !== "") {
showme.innerHTML = '<h1> loading.....</h1>';
var hr = new XMLHttpRequest()
{
hr.open("post", "index.php", true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.readystatechange = function () {
if (hr.readystate == 4 && hr.status == 200) {
showme.innerHTML = hr.responseText;
}
}
var v = "username=" + user;
hr.send(v);
};
}
}
您错过了}
函数中的一个结束括号callme()
。