2010-08-10 53 views
0

我正在創建一個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>"; 
} 

回答

0

你可以做類似的設置形式爲禁用,然後啓用它,當他們」 VE選擇登錄可用:

if (xmlhttp.readyState==4 && xmlhttp.status==200) 
{ 
    document.getElementById("loginName").innerHTML=xmlhttp.responseText; 
    document.getElementById('id-of-form').disabled = false; 
} 

我個人不會擔心太多,因爲你必須重新檢查反正它表單提交,因爲如果)他們有JS關閉或b)選擇相同登錄成爲其他即將加入的人(競賽條件),則需要馬上登錄確保他們都不成功。檢查登錄名是一種可用性功能,可幫助人們不會恨你和你的網站,使他們重新提交表單幾十次以找到一個好的登錄名。

0

我想這應該做
創建這樣

<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; 
      } 
...