Javascript AJAX用户名检查未验证
PHP/MySQL。 我创建了一个包含用户名文本字段和按钮的简单表单。页面名称为checkusername.php。我还编写了一个AJAX函数。然后我有另一个页面usercheckpage.php,其中checkusername.php的值通过AJAX函数Javascript AJAX用户名检查未验证,javascript,php,mysql,ajax,html,Javascript,Php,Mysql,Ajax,Html,PHP/MySQL。 我创建了一个包含用户名文本字段和按钮的简单表单。页面名称为checkusername.php。我还编写了一个AJAX函数。然后我有另一个页面usercheckpage.php,其中checkusername.php的值通过AJAX函数getResult()传递 在usercheckpage.php中,在数据库表中检查用户名,如果用户名已经存在,则显示“用户名不可用”,如果可用,则显示“用户名可用” 但问题是,即使显示警告消息'username not available',
getResult()
传递
在usercheckpage.php中,在数据库表中检查用户名,如果用户名已经存在,则显示“用户名不可用”
,如果可用,则显示“用户名可用”
但问题是,即使显示警告消息'username not available'
,如果我单击提交按钮,该值也会传递到下一个页面insertusername.php。如果用户名不可用,我不希望表单被提交。我尝试了html5验证。但效果不太好
这是我的密码:
checkusername.php
<form id="form1" name="form1" method="post" action="../controller/insertusername.php">
label for="txtuname"></label>
<input type="text" name="txtuname" id="txtuname" required onblur="getResult(this.value);" />
<span id="showusername"></span>
<input type="submit" name="submit" id="submit" value="SUBMIT" />
</form>
<?php
include '../model/Query.php';
$un=$_GET["variable"];
$name="select username from login where username='".$un."'";
$res=getData($name);
while($row=mysqli_fetch_array($res))
{
$name1=$row["username"];
}
if($un==$name1)
{
echo "name already exist";
}
else
{
echo "ok";
}
?>
checkusername.php
<form id="form1" name="form1" method="post" action="../controller/insertusername.php">
label for="txtuname"></label>
<input type="text" name="txtuname" id="txtuname" required onblur="getResult(this.value);" />
<span id="showusername"></span>
<input type="submit" name="submit" id="submit" value="SUBMIT" />
</form>
<?php
include '../model/Query.php';
$un=$_GET["variable"];
$name="select username from login where username='".$un."'";
$res=getData($name);
while($row=mysqli_fetch_array($res))
{
$name1=$row["username"];
}
if($un==$name1)
{
echo "name already exist";
}
else
{
echo "ok";
}
?>
HTML
附加提交事件
<form id="form1" name="form1" method="post" action="../controller/insertusername.php" onsubmit="checkUsername()">
<form id="form1" name="form1" method="post" action="../controller/insertusername.php" onsubmit="checkUsername()">
然后创建一个非常简单的函数,onsubmit
检查用户名是否可用。如果没有,只需返回false
,表格将不会提交
function checkUsername(){
var username = document.getElementById("txtuname").value;
if(!getResult(username))
return false;
}
function checkUsername(){
var username = document.getElementById("txtuname").value;
if(!getResult(username))
return false;
}
希望这对您有所帮助:-)若要正常工作,请将按钮更改为
<input type="button" value="Submit" onclick="check()">
我希望它能工作…HTML
附加提交事件
<form id="form1" name="form1" method="post" action="../controller/insertusername.php" onsubmit="checkUsername()">
<form id="form1" name="form1" method="post" action="../controller/insertusername.php" onsubmit="checkUsername()">
然后创建一个非常简单的函数,onsubmit检查用户名是否可用。如果没有,只需返回false,表格将不会提交
function checkUsername(){
var username = document.getElementById("txtuname").value;
if(!getResult(username))
return false;
}
function checkUsername(){
var username = document.getElementById("txtuname").value;
if(!getResult(username))
return false;
}
$un=$\u GET[“变量”]
应该是$un=$\u GET[“txtName”]
您需要将getResult
结果应用于表单submit
事件以操作表单提交。根据用户名的可用性启用或禁用提交按钮如何?我已将TxtName的值存储到getResult()中名为“variable”的变量。检查函数底部的第三行。所以这是正确的。我尝试在checkusername.php中回显$un的值,它给出了我在第一页中输入的用户名。我想实现我们在社交网站中看到的相同用户名检查。除了禁用sumbit按钮,您还有其他建议吗???@伊恩·布林德利