如何在php中通过ajax设置标志或指标?
问题描述:
我正在创建一个join now
页面。我正在使用登录名,密码,确认密码和电子邮件进行注册。这些输入我验证通过javascript,我检查login name
确实存在或不通过ajax.If它存在,我只是显示一个味精this id exists already please chose another one.
问题:是,如果用户键入登录ID已存在,ajax会显示已存在,但用户仍然可以点击提交,因为这段时间JavaScript会通过它,因为登录ID字段不为空.even我可以在服务器端db进程检查这个,但是I want to set one indicator or flag kind of, until and unless user enters such login id that does not exist he wont be able to submit form.
我的ajax代码 -如何在php中通过ajax设置标志或指标?
...
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("loginName").innerHTML=xmlhttp.responseText;
}
}
...
db文件 -
//check in user table
if($num_rows == 0)
{
echo "<font color='Green'>Ok, you can chose <b>$logid</b> as Login name.</font>";
}
elseif($num_rows >= 1)
{
echo "<font color='Red'><b>$logid</b> Ligin name has already been taken, Please chose another Login name.</font>";
}
或者我怎么可以设置登录ID文本框为空字符串与阿贾克斯的错误味精一起?一些喜欢 -
elseif($num_rows >= 1)
{
?>
<script>
document.getElementById("newid").value="";
</script>
<?php
echo "<font color='Red'><b>$logid</b> Ligin name has already been taken, Please chose another Login name.</font>";
}
答
你可以做类似的设置形式为禁用,然后启用它,当他们” VE选择登录可用:
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("loginName").innerHTML=xmlhttp.responseText;
document.getElementById('id-of-form').disabled = false;
}
我个人不会担心太多,因为你必须重新检查反正它表单提交,因为如果)他们有JS关闭或b)选择相同登录成为其他即将加入的人(竞赛条件),则需要马上登录确保他们都不成功。检查登录名是一种可用性功能,可帮助人们不会恨你和你的网站,使他们重新提交表单几十次以找到一个好的登录名。
答
我想这应该做
创建这样
<form name="name1" method="post" action="url" onsubmit="return checkForm(this)">
<!-- your fields here -->
<!-- add an hidden field -->
<input type="hidden" id="hidden1" name="hiddenf" value="0" />
</form>
<script>
function checkForm(form)
{
return form.hiddenf.value;
}
</script>
然后在Ajax回调函数形式
...
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("loginName").innerHTML=xmlhttp.responseText;
if(xmlhttp.responseText == 'not found') //your exist-id-message here
document.getElementById('hidden1').value = 0;
else
document.getElementById('hidden1').value = 1;
}
...